Message Basics

The PubNub messaging system powers real-time data flow. It delivers and processes messages within milliseconds.

Send messages

Send messages with the Publish API. It minimizes latency and maximizes delivery reliability across PubNub’s global network. You send to a single channel. Use JSON for structured data.

pubnub.publish(
{
channel: "my_channel",
message: {"text": "Hello World!"}
},
function(status, response) {
console.log(status, response);
}
);

Messages can include metadata for extra context. You can only publish to one channel at a time. For multi-channel operations, use channel groups.

The maximum message size is 32 KiB. This includes the channel name and any metadata. Larger messages return an error. This protects performance and network usage.

Signals

Signals are lightweight, one-way notifications. Use them for high-frequency, low-cost events like typing indicators or location pings. Signals do not support Message Persistence or Mobile Push Notifications.

See Messages vs Signals for details.

Receive messages

To receive messages, add an event listener with your SDK. Handlers process message and presence events so your app can react quickly.

const subscription = pubnub.channel('channel_1').subscription();
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };

Learn more about channels, presence, and message history.

Message actions

Message actions let you add actions, such as delivery acknowledgments or emoji reactions, after publish. You can add or remove these actions to improve user interaction, for example in chat messages.

pubnub.addMessageAction(
{
channel: 'chats.room1',
messageTimetoken: '15610547826970040',
action: {
type: 'reaction',
value: 'smiley_face',
}
},
function(status, response) {
console.log(status, response);
}
);

Message types

Use descriptive message types to categorize and process messages. Define a custom_message_type for every message to enable targeted handling and data management.

Filter by message type

You can use message types to filter messages using server-side subscribe filters.

Last updated on