Messages are the actual packages of data that get published to a channel. Messages can be chat messages, typing on/off events, location updates, emojis, or anything that your application publishes.
The message doesn't have to be formatted as JSON, but we recommend it. Most developers use the JSON format, and most PubNub SDKs automatically stringify JSON objects before publishing. Here is an example of a JSON message:
Publish messages in a channel
publish() method to publish messages in a channel. You can publish a message as a string, or send any data (JSON data, or an object), as long as the data size is below 32 KiB.
Receive messages in channels
If users are subscribed to a channel, listener events are triggered in the SDK when a new message is received in that channel. The message includes the publish payload, the sender's UUID and the timetoken of when the message was published.
For more info on event listeners and subscriptions go here.
The subscribe listener returns an envelope containing a 17-digit precision Unix time (UTC). To convert the timestamp to a UNIX epoch time (in seconds), divide it by 10,000,000. Or, you can use it directly from 17-digit precision time without converting. The format of
localeDateTime is something similar to
7/5/2019, 3:58:43 PM.
You only receive messages on channels to which you are subscribed. To avoid missing messages, add the message listener before calling subscribe().
Send images and files
publish() method allows you to publish messages up to 32 KiB, so if the file is small enough, you can encode a binary file in base64 format and publish the data as usual.
When the file size is above 32 KiB, you'll need to provide your own storage servers. You can store the binary files in a database such as Amazon S3 (or other cloud storage services), and then publish the URL along with a smaller thumbnail image over PubNub.
You should calculate your message size before publishing to PubNub to prevent "Message Too Large" gateway responses.
Send announcements to all users
You can send announcements to all users, or a limited set of users, in your application by using a channel to which the announcer has read/write access, and to which all other users have read-only access.
Once you have the channel set up, you can display any messages sent on the channel as announcements. Users with read access can subscribe to the channel to receive announcements, but can't send messages on the same channel.
Update messages in a channel
Since PubNub stores all messages in a time-series database, it doesn’t expose an API to directly update a message. Here are two good approaches to allow users to update messages:
Update messages by publishing another version
You can send a new version of a message on the same channel just like a normal publish. The new message must include the timetoken of the original message, and to simplify rendering, the updated message should contain the full content so that the original isn't needed.
One limitation of this approach is that the original and updated messages aren't linked together when they're stored in PubNub Storage. When you fetch messages on a channel, the method returns original message and updated messages independently, in the order in which they were published.
Update messages using message actions
You can use
addMessageAction() method to add metadata as an action to a message after it has been published. These actions are automatically stored and tied to the original message. So, when you fetch messages from storage you can also fetch associated actions in the same call.
You can add a maximum of 25,000 actions to a message. The maximum character length for the
type is 10 characters;
value is limited to 40 characters.
Delete messages in a channel
deleteMessages method permanently deletes messages from storage in a given channel. You can delete a single message, or a range of messages.
To delete a single message, set the
end parameters to the same timetoken value. To delete a range of messages, specify the timetoken of the first message in the
start parameter, and the timetoken of the last message in the
Message deletion is typically a server-side operation.