Receive Messages

To receive a message, your client must implement a listener and subscribe to a channel in which the message is being published.

Add a Listener

Messages and events are received in your app using a listener. This listener allows a single point to receive all messages, signals, and events.

Each type of message or event has its own handler to receive them where you implement your custom app logic to do something useful with the received data.

Once you no longer need to handle events in your app, you can remove a listener using a corresponding SDK method. See Javascript SDK as an example.

The following handlers are available to you:

User ID / UUID

User ID is also referred to as UUID/uuid in some APIs and server responses but holds the value of the userId parameter you set during initialization.

pubnub.addListener({
message: function(m) {
// handle message
var channelName = m.channel; // The channel to which the message was published
var channelGroup = m.subscription; // The channel group or wildcard subscription match (if exists)
var pubTT = m.timetoken; // Publish timetoken
var msg = m.message; // The Payload
var publisher = m.publisher; //The Publisher
},
presence: function(p) {
// handle presence
var action = p.action; // Can be join, leave, state-change, or timeout
var channelName = p.channel; // The channel to which the message was published
var occupancy = p.occupancy; // Number of users subscribed to the channel
var state = p.state; // User State
show all 67 lines

The following is an overview of each of the listener's event handlers. Details about the data passed to the handlers' parameters will be explained in the API sections of the associated handlers.

HandlerDescription
StatusReceives events when the client successfully connects (subscribes), or reconnects (in case of connection issues) to channels. For other error events that can be received by the Status handler, refer to Connection Management and to the tip that follows this table.
MessageReceives all messages published to all the channels subscribed by the client. The event payload will contain the published message data, publish timetoken, the User ID of the client that published the message and more. Refer to Publish Messages.
SignalReceives all signals that are sent to any channels subscribed by the client. The event payload will contain the signal data, signal timetoken, the User ID of the client that published the message and more. Refer to Send Signals.
PresenceReceives all presence events that are triggered on any channels subscribed by the client. The event payload will contain the presence event type (join, leave, timeout, state-change), timetoken of the action, the User ID of the client that caused the event, state data (if applicable) and more. Refer to Detect Presence.
MessageActionReceives all events when existing messages are annotated (by an action) for any channels subscribed by the client. The event payload will contain the User ID that acted on the message, message action type (such as an emoji reaction), timetoken of the annotated message, timetoken of the action, action value and more. Refer to Message Reactions.
ObjectsReceives all objects events that are emitted when a channel, channel membership, or user metadata is created, updated or removed. Refer to Channel Metadata, User Metadata, and Membership Metadata.
Status event handling

For more information on how the SDKs handle status events, refer to SDK troubleshooting and status event references.

Last updated on
On this page