Messages

Messages are packages of data that get published to your spaces. They can contain any serializable data, including objects, arrays, numbers, and strings. String content can include any UTF-8 character, single-byte and multi-byte. PubNub's maximum size for a single message is 32 KiB.

While messages can be published in any format, JSON is usually the chosen format. The data can be any serializable JSON object; the PubNub SDKs automatically stringify JSON objects before publishing. When you use JSON, the keys and values are up to you.

Don't JSON serialize

You shouldn't JSON serialize when sending signals/messages via PubNub. The serialization is done for you automatically. Instead just pass the full object as the message payload. PubNub takes care of everything for you.

State Shape

The data about an individual message is stored as a slice in a normalized pool of Messages. The following example shows the shape of a list of Messages in the store:

{
"byId": {
"introductions": [
{
"channel": "introductions",
"subscription": {},
"actualChannel": {},
"subscribedChannel": "introductions",
"timetoken": "15724660884258083",
"publisher": "pn-f3164dfb-dbde-4c6a-8d7e-e939a2c4dadb",
"message": {
"content": {"type": "text","body": "Hello everybody!"},
"sender": "glampartg"
}
},
show all 56 lines

Reducers

The PubNub Redux framework provides reducers your app can implement that respond to various actions that update the store. To track the state of a set of objects in the store, combine the reducers you want into the rootReducer for your app.

createMessageReducer

createMessageReducer instantiates a reducer in the store that responds to actions dispatched to update the state of messages in the store.

createMessageReducer();

Listeners

The PubNub Redux framework includes listeners that monitor PubNub events from the server and dispatch corresponding actions. All listeners are automatically invoked if your app registers the combined PubNub listener. You can register only specific listeners, or implement your own combine listeners function.

createMessageListener

createMessageListener registers a listener in the store that monitors when messages are received from the server, and dispatches corresponding actions to update the store.

A sample implementation of a single listener:

pubnub.addListener(createMessageListener(store.dispatch));

Commands

The PubNub Redux framework provides commands that your app can dispatch to the store to be managed by the Thunk middleware. Commands interact with the PubNub API and dispatch basic actions which are then processed by reducers to update the state in the store.

sendMessage

Send a message packaged in a request object.

sendMessage( request, [meta] );

sendMessage Arguments

ParameterTypeRequiredDescription
requestMessageContentTypeYesMessage content request parameter object.
[meta]objectOptionalStandard meta options object.

MessageContentType Properties

PropertyTypeRequiredDefaultDescription
messageobjectYesThe message content may be any valid JSON type including objects, arrays, strings, and numbers.
channelstringYesThe channel name to publish messages to. If you use the same string for the spaceId as the channel ID, then channel would be the same as the spaceId.
[storeInHistory]booleanOptionaltrueIf true, the messages are stored in history. If not specified, the history configuration on the key is used.
[meta]objectOptionalMeta data object which can be used with the filtering capability.
[ttl]numberOptional

Set a per message time to live in Message Persistence:

  • If storeInHistory = true, and ttl = 0, the message is stored with no expiry time.
  • If storeInHistory = true and ttl = X (an Integer value), the message is stored with an expiry time of X hours.
  • If storeInHistory = false, the ttl parameter is ignored.
  • If ttl isn't specified, the message expiration defaults to the expiry value for the key.

sendMessage Sample Usage

dispatch(sendMessage({
message: {
content: {
type: 'text',
body: 'How is everyone doing today?'
},
sender: 'user_6a535a77e2aa4a109373ac6670d6c2a3'
}
channel: 'space_ac4e67b98b34b44c4a39466e93e',
}
}));

fetchMessageHistory

Fetch past messages on a channel.

fetchMessageHistory( request, [meta] )

fetchMessageHistory Arguments

ParameterTypeRequiredDescription
requestMessageHistoryRequestOptionsYesMessage content request parameter object.
[meta]objectOptionalStandard meta options object.
MessageHistoryRequestOptions Properties
PropertyTypeRequiredDefaultDescription
channelstringYesSpecifies channel to return history messages from.
[reverse]booleanOptionalfalseSetting to true will traverse the time line in reverse starting with the oldest message first.

If both start and end arguments are provided, reverse is ignored and messages are returned starting with the newest message.
[count]numberOptional100
[stringifiedTimeToken]booleanOptionalfalseIf stringifiedTimeToken is specified as true, the SDK will return timetoken values as a strings instead of integers. Usage of setting is encouraged in JavaScript environments which perform round-up/down on large integers.
[includeMeta]booleanOptionalWhether message meta information should be fetched or not.
[start]stringOptionalTimetoken delimiting the start of time slice (exclusive) to pull messages from.
[end]stringOptionalTimetoken delimiting the end of time slice (inclusive) to pull messages from.

fetchMessageHistory Sample Usage

dispatch(fetchMessageHistory({
channel: 'space_ac4e67b98b34b44c4a39466e93e',
reverse: true,
count: 50,
stringifiedTimeToken: true,
includeMeta: true,
start: '14867650866860160',
end: '14867650866860159'
}));
Last updated on