Skip to content

Migration guide: ChatObserver and events() APIs

Márton Braun edited this page Feb 22, 2021 · 3 revisions

The API for handling events from Chat has been updated to make it simpler to use - and harder to misuse. We strongly suggest migrating to the new APIs.

The main change is that the intermediate ChatObservable class is being removed, and instead of chaining operators onto that and then subscribing to it, you can call subscribe-like methods on the ChatClient or ChannelController objects directly.

Here's how calls to the old API are replaced with the new API.

Please note that while the examples here are for the ChatClient, the API changes and migration steps are exactly the same for ChannelController.

Subscribing to all events

Old API

client.events().subscribe { event: ChatEvent ->
    if (event is NewMessageEvent) {
        val message = event.message
    }
}

New API

client.subscribe { event: ChatEvent ->
    if (event is NewMessageEvent) {
        val message = event.message
    }
}

Disposing a subscription (unsubscribing from events)

Old API

val subscription: Subscription = client.events().subscribe { /* ... */ }
subscription.unsubscribe()

New API

val disposable: Disposable = client.subscribe { /* ... */ }
disposable.dispose()

Filtering for event types

Filtering by the type field, by Strings

Old API

client.events()
    .filter("message.new")
    .filter("message.updated")
    .filter("message.deleted")
    .subscribe { event: ChatEvent ->
        /* ... */
    }
client.events()
    .filter("message.new")
    .filter("message.updated")
    .filter("message.deleted")
    .subscribe((ChatEvent event) -> {
        /* ... */
        return Unit.INSTANCE;
    });

New API

client.subscribeFor(
    "message.new",
    "message.updated",
    "message.deleted"
) { event: ChatEvent ->
    /* ... */
}
client.subscribeFor(
    new String[]{"message.new", "message.updated", "message.deleted"},
    (ChatEvent event) -> {
        /* ... */
        return Unit.INSTANCE;
    });
Filtering for built-in types

Old API

client.events()
    .filter(EventType.MESSAGE_NEW)
    .subscribe { event: ChatEvent ->
        /* ... */
    }

New API

client.subscribeFor(EventType.MESSAGE_NEW) { event: ChatEvent ->
    /* ... */
}
Filtering by event class

Old API

client.events()
    .filter(NewMessageEvent::class.java)
    .subscribe { event: ChatEvent ->
        /* ... */
    }
client.events()
    .filter(NewMessageEvent.class)
    .subscribe((ChatEvent event) -> {
        /* ... */
        return Unit.INSTANCE;
    });

New API

client.subscribeFor(NewMessageEvent::class) { event: ChatEvent ->
    /* ... */
}
client.subscribeFor<NewMessageEvent> { newMessageEvent: NewMessageEvent ->
    /* ... */
}
client.subscribeFor(
    new Class[]{NewMessageEvent.class},
    (ChatEvent event) -> {
        /* ... */
    });
Custom filtering

Old API

client.events()
    .filter { it.type == "my.custom.event" && it.createdAt < someDate }
    .subscribe { event: ChatEvent ->
        /* ... */
    }

New API

client.subscribe { event: ChatEvent ->
    if (event.type == "my.custom.event" && event.createdAt < someDate) {
        /* ... */
    }
}

Listening for a single event

Old API

client.events()
    .filter(ConnectedEvent::class.java)
    .first()
    .subscribe { event: ChatEvent
        Toast.makeText(this, "First connection", Toast.LENGTH_SHORT).show()
    }
client.events()
        .filter(ConnectedEvent.class)
        .first()
        .subscribe((ChatEvent event) -> {
            /* ... */
            return Unit.INSTANCE;
        })

New API

client.subscribeForSingle(ConnectedEvent::class.java) { event: ConnectedEvent ->
    Toast.makeText(this, "First connection", Toast.LENGTH_SHORT).show()
}
client.subscribeForSingle<ConnectedEvent> { event: ConnectedEvent
    Toast.makeText(this, "First connection", Toast.LENGTH_SHORT).show()
}
client.subscribeForSingle(ConnectedEvent.class,
    (ConnectedEvent event) -> {
        /* ... */
    });