Listening for Events

As soon as you call watch on a Channel or queryChannels you’ll start to listen to these events. You can hook into specific events:

// There are 2 ways to listen for events
// 1. Use an EventsController
class MyViewController: UIViewController, EventsControllerDelegate {
  var client: ChatClient!
  var eventsController: EventsController!

  override func viewDidLoad() {
    super.viewDidLoad()
    eventsController = client.eventsController()
    eventsController.delegate = self
  }

  func eventsController(_ controller: EventsController, didReceiveEvent event: Event) {
    // Handle any event received
    switch event {
    case let event as MessageNewEvent:
      // handle MessageNewEvent
    default:
      break
    }
  }
}
// 2. Use the appropriate controller's delegate methods
// ChannelController delegate methods to listen Channel events
// CurrentUserController delegate methods to listen Current User events
/// * Delegates *

class ChannelViewController: ChatChannelControllerDelegate {
  func channelController(_ channelController: ChatChannelController, didUpdateMessages changes: [ListChange<ChatMessage>]) {
    // animate the changes to the message list
  }
}

let channelViewController = ChannelViewController()

channelController.delegate = channelViewController

/// * Combine *

channelController.messagesChangesPublisher
  .receive(on: RunLoop.main)
  .sink { changes in
    // animate the changes to the message list
  }
  .store(in: &cancellables)

You can also listen to all events at once: (Full list of events is on events object page)

// There are 2 ways to listen for events
// 1. Use an EventsController
class MyViewController: UIViewController, EventsControllerDelegate {
  var client: ChatClient!
  var eventsController: EventsController!

  override func viewDidLoad() {
    super.viewDidLoad()
    eventsController = client.eventsController()
    eventsController.delegate = self
  }

  func eventsController(_ controller: EventsController, didReceiveEvent event: Event) {
    // Handle any event received
    switch event {
    case let event as MessageNewEvent:
      // handle MessageNewEvent
    default:
      break
    }
  }
}
// 2. Use the appropriate controller's delegate methods
// ChannelController delegate methods to listen Channel events
// CurrentUserController delegate methods to listen Current User events

Client Events

Not all events are specific to channels. Events such as the user’s status has changed, the users’ unread count has changed, and other notifications are sent as client events. These events can be listened to through the client directly:

// There are 2 ways to listen for events
// 1. Use an EventsController
class MyViewController: UIViewController, EventsControllerDelegate {
  var client: ChatClient!
  var eventsController: EventsController!

  override func viewDidLoad() {
    super.viewDidLoad()
    eventsController = client.eventsController()
    eventsController.delegate = self
  }

  func eventsController(_ controller: EventsController, didReceiveEvent event: Event) {
    // Handle any event received
    switch event {
    case let event as MessageNewEvent:
      // handle MessageNewEvent
    default:
      break
    }
  }
}
// 2. Use the appropriate controller's delegate methods
// ChannelController delegate methods to listen Channel events
// CurrentUserController delegate methods to listen Current User events

Connection Events

The official SDKs make sure that a connection to Stream is kept alive at all times and that chat state is recovered when the user’s internet connection comes back online. Your application can subscribe to changes to the connection using client events.

let connectionController = client.connectionController()

class ConnectionDelegate: ChatConnectionControllerDelegate {
  func connectionController(_ controller: ChatConnectionController,
               didUpdateConnectionStatus status: ConnectionStatus) {
     // handle connection change
  }
}

connectionController.delegate = ConnectionDelegate()

Stop Listening for Events

It is a good practice to unregister event handlers once they are not in use anymore. Doing so will save you from performance degradations coming from memory leaks or even from errors and exceptions (i.e. null pointer exceptions)

// There are 2 ways to listen for events
// 1. Use an EventsController
class MyViewController: UIViewController, EventsControllerDelegate {
  var client: ChatClient!
  var eventsController: EventsController!

  override func viewDidLoad() {
    super.viewDidLoad()
    eventsController = client.eventsController()
    eventsController.delegate = self

    // stop listening for events
    eventsController = nil
  }

  func eventsController(_ controller: EventsController, didReceiveEvent event: Event) {
    // Handle any event received
    switch event {
    case let event as MessageNewEvent:
      // handle MessageNewEvent
    default:
      break
    }
  }
}
© Getstream.io, Inc. All Rights Reserved.