Receive messages

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

  1. Create a subscription or a subscription set.
  2. Implement the appropriate event listener.
  3. Subscribe to the subscription to start receiving real-time updates.

Before you begin, make sure you have initialized PubNub with your keys. PubNub delivers messages globally in under 30 ms and automatically recovers connections.

PubNub SDKs make this process easy with entities. Each entity provides operations for its own type. For example, Channel works on channels and UserMetadata works on user metadata. There are four entities you can use:

  • Channel
  • ChannelGroup
  • UserMetadata
  • ChannelMetadata
Entity-enabled SDKs

Not all PubNub SDKs currently support entities. Refer to each SDK's API documentation for more information.

You can subscribe to hundreds of channels with a single call. There are many ways of subscribing, so choose the option that fits your use case.

// create a subscription to a single channel
const channel = pubnub.channel('chats_inbox.user_1905')
const subscription1 = channel.subscription({ receivePresenceEvents: true });

// create a subscription to multiple channels
const channels = pubnub.subscriptionSet({
channels: [
"chats_guilds.mages_guild",
"alerts.system",
"geolocation.data"
]
});
const subscription2 = channels.subscription();

// subscribe to both subscriptions and
show all 18 lines

When you subscribe to a channel, your client receives all messages sent to it. To act on the data, implement an event listener.

The event listener is the single entry point for messages, signals, and other events on your subscribed channels. Each data type has its own handler. Use handlers to define what your app does when it receives that type. For example, display the message content or trigger business logic.

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).

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

To handle another message type, such as a signal, add a signal handler to your listener. Each handler has its own properties and maps to one message type.

The code below prints the message content and publisher using the message listener.

Every received message has an internal message type (immutable integer) and a custom message type (mutable string you define). These parameters let you filter, group, or apply conditional logic to different types of messages. The code below prints all messages.

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.

// add a subscription connection status listener
pubnub.addListener({
status: (s) => {console.log('Status', s.category) }
});

// add message and presence listeners
subscription.addListener({
// Messages
message: (m) => { console.log('Received message', m) },
// Presence
presence: (p) => { console.log('Presence event', p) },
});

You can enhance published messages by attaching emojis, actions, or delivery acknowledgements.

You can also persist and retrieve older messages, not only the ones sent in real time.

Last updated on
On this page