Message History
Message history stores realtime messages in a memory. When a user is subscribing to a room, they can also ask for past messages. When the message history feature is enabled Scaledrone will store up to 100 latest messages for each room, the messages will be stored for up to 30 days.
Message history is useful for many applications, such as:
- Getting past messages for a chat room application
- Keeping track of a live dashboard state.
- Receiving missed messages after a user disconnected.
Live Examples & Source Code
Getting Started
Before trying to use this feature enable the message history feature from the UI - you can do this on the channel's page in the dashboard.
You can ask for up to 100 past messages when subscribing to a room, but keep in mind that every message you receive from the history does count as one daily event. You can do this by passing a options object as the second parameter to the subscribe function containing the parameter history
.
JavaScript, Node.js and React Native libraries
To receive the messages listen to the history_message
event. The history_message
events will be triggered in order from the oldest to the newest message.
const room = drone.subscribe('awesome-historical-room', {
historyCount: 5 // ask for the 5 most recent messages from the room's history
});
room.on('history_message', message => console.log(message));
Android/Java library
Read more..iOS/Swift library
Read more..Securing history usage with JWT
If you are already using JWT Authentication, you have to define the history parameter on your room permissions. If you don't set the history parameter, your users will be unable to ask for past messages.
A basic JWT with the history permission would look like this:
{
"client": "client_id_sent_from_client",
"channel": "channel_id",
"permissions": {
"^awesome-historical-room$": {
"publish": true,
"subscribe": true,
"history": 10
}
},
"exp": 1408639878
}
The history parameter in a JWT will define the maximum amount of past messages the user can query. The user can ask for less or no messages at all but cannot ask for more than the permission allows.