Handling Messages History

Here we explain how Photon Cloud manages the history of chat messages and how you could handle that via chat clients.

Public Channels

Photon Cloud keeps the last published messages per channel as long as the channel exists. Empty channels are destroyed on the server after a timeout of few seconds: all messages are cleaned up. To persist history you could use Photon Chat WebHooks.

Photon Cloud keeps up to 100 messages in history per public chat channel.

When subscribing to a channel the chat client can set how many messages should be received from history, if any. This can be done using historyLen (also called messagesFromHistory) parameter:

  • -1: all missed messages, up to 100.
  • 0: none. (default)
  • 1 >= x >= 100: all missed messages up to x. This could be useful to avoid receiving a lot especially if you are going to display only a few or if you are worried that receiving and processing too many messages at once might cause some performance degradation.
  • Other values should not be allowed.

Each message published inside a channel has a unique ID. Message IDs are integers, start from 1 and are incremented with each new message published. A chat channel has limited history but the server keeps track of the last message ID published inside the channel per channel.

Example: If a user publishes the message n° 573, chronologically, counting all messages from all users since the creation of the channel:

  • the published message will have ID 573
  • the channel will have 100 messages in history: from MsgID=474 to MsgID=573
  • the channel will have Last Msg ID set to 573

If the client is recovering from unexpected disconnection and will subscribe back to the same channels or if the client is returning to an old channel after pausing the application, it makes sense to receive only the 'delta' or the 'diff' from the server. No need to get messages already received before. Only the messages published in the channel when the client was absent should be received; only the missed messages. Photon Chat client can tell the server, using lastMsgId (or lastMsgIDs if subscribing to multiple channels at the same time) parameter, the ID of the last message it has received from the server for that channel. This way the server can send back only the messages that were published after that messages, if any. Those messages have IDs higher than the lastMsgId sent. Of course, if the user stays disconnected for a long time and messages published during this absence exceed the history messages capacity then some messages will never make it to the client.

Example: User unsubscribes from a channel after receiving, in that same channel, the last message with ID 218. The channel reaches message ID 603 and has 100 messages in history with IDs from 504 to 603. If at this time, the same user subscribes back to the same channel he/she will have missed messages with IDs from 219 to 503.

The requirement for the proper usage of lastMsgId is that the client should keep track of the ID of the last message received per channel. Some client SDKs might save and expose the last message ID per channel for the whole application session but you need to cache those locally on the device or persist them on your own backend if needed.

You can also combine historyLen and lastMsgId parameters per channel to receive only the last missed messages, if any, all of them (historyLen = -1) or up to a certain number (0 < historyLen <= 100). However, it does not make sense to use historyLen == 0 with lastMsgId.

Example: User unsubscribes from a channel after receiving, in that same channel, the last message with ID 777. The channel reaches message ID 801 and has 100 messages in history with IDs from 702 to 801. The same user subscribes back to the same channel and sends lastMsgId = 777 and historyLen = -1. In this case, the server will send back the last 23 messages with IDs from 778 to 801.

Back To Top

Private Chat

Photon Chat allows sending private messages to anyone even offline users. If private messages are sent to an offline user, those messages will be kept on Photon Cloud until the user connects or the sender disconnects.

Photon Cloud keeps up to 100 messages in history for each private conversation between any couple of users as long as one of those two users is connected. When both users are disconnected the private conversation is deleted.

To Document Top