Receive messages

Receiving messages (or any other events for that matter) 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.

PubNub SDKs make the entire process easy by introducing objects we call entities

Entity

A subscribable object within a PubNub SDK that allows you to perform context-specific operations.
. They provide operations on endpoints associated with their type, so Channel works on channels, UserMetadata works on user metadata objects, and so on. 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 make sure you choose the optimal one for 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, the client starts to receive all messages sent to it. To act on the received data, you must implement an event listener.

The event listener is a single point through which your app receives all messages, signals, and events that are sent to any channel you are subscribed to. Each type of data sent to a channel has its own handler which is used to provide logic for what to do when the client receives a given message type. For example, you may want to simply display the content of the message or trigger custom business logic.

Add listeners to subscriptions and subscription sets for specific or multiple subscriptions simultaneously.

If you wanted to handle a different message type like a signal, you'd need to implement a signal handler in your listener. Each handler has its own set of available properties and is dedicated to a separate type of message you can send.

In the code below, the content and the publisher of each received message is handled by the message listener which prints them to the console.

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 messages that have already been published by attaching emojis, reactions, or delivery acknowledgements.

Even though PubNub is all about real-time communication, you can persist and retrieve older messages, not just the ones being sent in real time.

Last updated on
On this page