flutterpubnub

Using pubnub in flutter, how to initiate a chat?


I have a chat app and I am stuck while initiating the chat. According to pubnub, anytime I message to a new channel "A", that channel is created. My problem is that I want to subscribe to that channel and show the message but I cannot subscribe before the channel is created.


Solution

  • "Creating" PubNub Channels

    Channels are not really "created", channels are just string tokens for routing messages. So there is no notion of creating a channel in PubNub (not officially, anyway).

    The phrase "create a channel" is more of a concept than a real thing in PubNub. Within your app, creating a channel (a chat room) is a real thing. So you can subscribe to a channel that hasn't yet been "created".

    So when your app "creates" a channel (starts a new chat room), your participating clients can subscribe to the channel and messages can be published later.

    Or, clients can subscribe after messages are published and use fetchMessages to get any messages that were sent before the client subscribed.