Receive Messages

Messages sent through PubNub are PubNub events with a message payload. They are only structured differently.

Channels are unique identifiers that carry real-time messages between publishers and subscribers. See channels for details.

Receiving messages (and other events) is a three-step process:

  1. Create an entity-based subscription or a subscription set.
  2. Implement the appropriate event listener.
  3. Subscribe to the subscription to receive real-time updates.
Entity-enabled SDKs

Not all PubNub SDKs support entities yet. For SDKs that still use legacy event listeners and the Subscribe API, see their SDK API docs.

Refer to the API documentation of each SDK for more information.

Add an event handler

Messages and events are received using a listener (a callback function). In the listener, add logic for messages, signals, files, presence, and other events.

Add listeners to subscriptions and subscription sets to handle one or many subscriptions.

There are four entities:

Each message or event type has its own handler where you implement app logic for the received data.

Subscription with Presence

To receive Presence events, you subscribe with Presence and have Presence enabled on your keyset. Make sure you configure Presence to track Presence-related events for all or selected channels (through Presence Management rules).

When you no longer need to handle events, remove the listener using the SDK method. See JavaScript SDK as an example.

You can receive events using the following approaches:

You can assign functions directly to event-specific properties on the subscription object. This approach provides a clean, straightforward way to handle specific events.

const channelSubscription = pubnub.channel('channel_name').subscription();
channelSubscription.subscribe();

// Handle messages
channelSubscription.onMessage = function(message) {
console.log('Received message:', message);
};

// Handle presence events
channelSubscription.onPresence = function(presenceEvent) {
console.log('Presence event:', presenceEvent);
};

// Other handlers are available for signals, objects, files, and message actions

This method allows for clear, direct assignment of handlers to specific event types and is particularly useful when you want to add or change handlers dynamically.

For more information on how to use the on[Event] syntax, refer to each SDK's API documentation, for example, JavaScript.

The following handlers are available:

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.

// create a subscription from a channel entity
const channel = pubnub.channel('channel_1');
const subscription = channel.subscription();

// Event-specific listeners
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
subscription.onSignal = (signalEvent) => { console.log("Signal event: ", signalEvent); };
subscription.onObjects = (objectsEvent) => { console.log("Objects event: ", objectsEvent); };
subscription.onMessageAction = (messageActionEvent) => { console.log("Message Action event: ", messageActionEvent); };
subscription.onFile = (fileEvent) => { console.log("File event: ", fileEvent); };

// Generic listeners
show all 76 lines

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

Subscription with Presence

To receive Presence events, you subscribe with Presence and have Presence enabled on your keyset. Make sure you configure Presence to track Presence-related events for all or selected channels (through Presence Management rules).

HandlerDescription
Status
Receives events when the client connects (subscribes) or reconnects to channels. For other error events the Status handler can receive, see Connection Management and the tip that follows this table.

In entity-enabled SDKs, add this listener to the pubnub object, not to an individual subscription.
Message
Receives all messages published to channels the client subscribes to. The event payload contains the published message data, publish timetoken, the user ID of the publisher, and more. See Publish Messages.
Signal
Receives all signals sent to any subscribed channels. The event payload contains the signal data, signal timetoken, the user ID of the publisher, and more. See Send Signals.
Presence
Receives presence events on subscribed channels. Event types include join, leave, timeout, and state-change. The payload includes a timetoken for the action, the user ID, and optional state data. See Detect Presence.
MessageAction
Receives events when messages are annotated by an action on any subscribed channels. The payload includes the user ID that acted, action type (such as an emoji reaction), timetoken of the annotated message, timetoken of the action, action value, and more. See Message Actions.
Objects
Receives objects events when channel, membership, or user metadata is created, updated, or removed. See App Context.
Status event handling

PubNub automatically tries to reconnect after transient network issues. For more about status events and troubleshooting, see SDK troubleshooting and status event references.

Last updated on