Custom Events

Custom events allow you to build more complex interactions within a channel or with a user.

To a channel

Users connected to a channel, either as a watcher or member, can send custom events and have them delivered to all users watching the channel.

struct MyCustomEvent: CustomEventPayload {
  static let eventType: EventType = "my_custom_event"

  let text: String
}

// Send channel events using the channel controller
let event = MyCustomEvent(text: "This is my custom text")
channelController.eventsController().sendEvent(event)

// Subscribe for custom events works with delegation pattern
extension MyChannelViewModel: EventsControllerDelegate {

  private var eventsController: EventsController?

  func setupEventListening(channelController: ChatChannelController) {
    // eventsController needs to be retained (eg. class var)
    eventsController = channelController.eventsController()
    eventsController?.delegate = self
  }

  // EventsControllerDelegate method
  func eventsController(_: EventsController, didReceiveEvent event: Event) {
    // ignore regular events, we only care about custom channel event
    guard let channelEvent = event as? UnknownChannelEvent else {
      return
    }

    // Filter only `MyCustomEvent` event
    guard let customPayload = channelEvent.payload(ofType: MyCustomEvent.self) else {
      return
    }

    print("Received a custom event with text \(myCustomPayload.text)")
  }
}

Custom events are enabled by default on all channel types, you can disable them using the Dashboard or the API the same way as you would manage other channel features (ie. replies, URL previews, …)

Permissions

Like every client-side API request, sending a custom event includes a permission check. By default users that can read messages on a channel can also send custom events. Check the Auth & Permission section to find out more about how permissions can be customized for your application.

Keep in mind a clever user can send their own custom events. We recommend using the type attribute or custom data on the event to limit the kinds of events that are displayed to the recipient to ones that are safe e.g. if a bad actor sends a “password reset” or other malicious events, your client app should just ignore it.

To a user

This allows you to send custom events to a connected user. The event is delivered to all connected clients for that user.

It is only available with server-side authentication. A copy of the event is sent via web-hooks if it is enabled.

await client.sendUserCustomEvent(targetUserID, {
  type: "friendship_request",
  text: "Tommaso wants to be your friend",
});
nametypedescriptiondefaultoptional
targetUserIDstringtarget user ID-
dataobjectevent to be sent-
data.typestringtype of the event-

If the user doesn’t exist, a 404 Not Found error is returned.

The type of the event shouldn’t contain any . character otherwise a 400 Bad Request error is returned. This a character used for built-in events, see Event Object section for more details.

© Getstream.io, Inc. All Rights Reserved.