Users can subscribe to channels to receive messages on them. Subscribing to channels initiates a realtime connection with PubNub. This connection is kept open for the duration the user stays subscribed from a client application. Any user subscribing to a channel receives messages in under 100ms, regardless of which global region the message was published in.
PubNub allows you to store the association between users and channels so clients can get channel memberships for a user or show members in a channel.
Users can subscribe to channels and begin listening for messages on a single socket connection. You can subscribe to a list of up to 100 channels, or by using channel groups to subscribe to the list of channels in a channel group. Using channel groups lets you subscribe to thousands of channels.
Typically, you subscribe to channels on app load. As a user moves around in the app, they stay subscribed to their list of channels so they can continue to receive messages on all their channels.
Subscribing to channels
subscribe() method to subscribe to a list of channels (up to 100 channels). Use the status listener to be notified of connectivity. Your application should wait for a
PNConnectedCategory status event before publishing a message to avoid a potential race conditions.
Wildcard subscribes allow you to subscribe to multiple channels using wildcard. For example, if you subscribe to
a.* you will get all messages for channels
a.b, a.c, a.x. The wildcarded
* portion refers to any portion of the channel string name after the dot (.).
Unsubscribing from channels
Use this method to unsubscribe from channels and stop receiving messages on them.
Unsubscribing from all channels
Use this method to unsubscribe from all channels and disconnect from PubNub. You should do this before your application closes or moves to the background so the connection with PubNub can close gracefully.
Managing Channel Groups
Channel groups allow you to bundle thousands of channels into a group that can be identified by name. When you subscribe to a channel group, you receive data from all the channels the channel group contains.
Subscribing to a channel group
Use this method to subscribe to channel groups.
Adding channels to a channel group
Use this method to add channels to a channel group.
Removing channels from a channel group
Use this method to remove channels from a channel group.
Listing channels in a channel group
Use this method to list all the channels in a channel group.
PubNub allows you to store the association between users and channels so clients can get channel memberships for a user, or show members in a channel. Users don't need to join channels to subscribe and start exchanging messages on them, but it can be a helpful mechanism for your application to use.
Use this method to join a list of channels. When a user joins a channel, the relationship between the user and the channel is stored in the database and can be retrieved in future.
Use this method to remove a user from a list of channels.
Use this method to retrieve the specified user's channel memberships.
Use this method to update a user's channel memberships.
Getting members of a channel
Use the methods in this section to get the list of members in a channel.
Adding members to a channel
Use this method to add one or more members to a channel.
Removing members from a channel
Use this method to remove a list of members from a channel.