Messages API
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:
1{
2 "byId": {
3 "introductions": [
4 {
5 "channel": "introductions",
6 "subscription": {},
7 "actualChannel": {},
8 "subscribedChannel": "introductions",
9 "timetoken": "15724660884258083",
10 "publisher": "pn-f3164dfb-dbde-4c6a-8d7e-e939a2c4dadb",
11 "message": {
12 "content": {"type": "text","body": "Hello everybody!"},
13 "sender": "glampartg"
14 }
15 },
show all 56 linesReducers
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.
1createMessageReducer();
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:
1pubnub.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.
1sendMessage( request, [meta] );
sendMessage arguments
MessageContentType properties
messagerequired#- The message content may be any valid JSON type including objects, arrays, strings, and numbers.
channelrequired#- The channel name to publish messages to. If you use the same string for the
spaceIdas the channel ID, thenchannelwould be the same as thespaceId. [storeInHistory]#- If
true, the messages are stored in history. If not specified, the history configuration on the key is used. [meta]#- Meta data object which can be used with the filtering capability.
[ttl]#Set a per message time to live in Message Persistence:
- If
storeInHistory=true, andttl=0, the message is stored with no expiry time. - If
storeInHistory=trueandttl=X(an Integer value), the message is stored with an expiry time ofXhours unless you have message retention set toUnlimitedon your keyset configuration in the Admin Portal. - If
storeInHistory=false, thettlparameter is ignored. - If
ttlisn't specified, the message expiration defaults to the expiry value for the key.
- If
sendMessage sample usage
1dispatch(sendMessage({
2 message: {
3 content: {
4 type: 'text',
5 body: 'How is everyone doing today?'
6 },
7 sender: 'user_6a535a77e2aa4a109373ac6670d6c2a3'
8 }
9 channel: 'space_ac4e67b98b34b44c4a39466e93e',
10 }
11}));
fetchMessageHistory
Fetch past messages on a channel.
1fetchMessageHistory( request, [meta] )
fetchMessageHistory arguments
MessageHistoryRequestOptions properties
channelrequired#- Specifies
channelto return history messages from. [reverse]#- Setting to
truewill traverse thetimeline inreversestarting with the oldestmessagefirst.
If bothstartandendarguments are provided,reverseis ignored and messages are returned starting with the newestmessage. [count]#[stringifiedTimeToken]#- If
stringifiedTimeTokenis specified astrue, the SDK will returntimetokenvalues as a strings instead of integers. Usage of setting is encouraged in JavaScript environments which perform round-up/down on large integers. [includeMeta]#- Whether message meta information should be fetched or not.
[start]#- Newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. Must be a higher timetoken value than
endwhen both are provided. [end]#- Older boundary of the query (inclusive). Retrieval stops at this timetoken. Must be a lower timetoken value than
startwhen both are provided.
fetchMessageHistory sample usage
1dispatch(fetchMessageHistory({
2 channel: 'space_ac4e67b98b34b44c4a39466e93e',
3 reverse: true,
4 count: 50,
5 stringifiedTimeToken: true,
6 includeMeta: true,
7 start: '14867650866860160',
8 end: '14867650866860159'
9}));