I don't understand why I see so many presence messages in my stats.

Ably's presence is a powerful feature that allows client devices to subscribe in real time to presence events such as as when users enter, update or leave a channel.  Each of these presence updates is published as a presence message on a channel so that all clients on a channel can keep an up to date list of who is present on a channel at any time.

 

As each presence update is counted as a single message, this can lead to a surprising number of messages being published and subscribed to in a very short amount of time.

 

For example, if 200 people were subscribing to presence updates on a channel, and all entered in one minute, and then left within that same minute, this would result in:

  • 200 presence messages being published for the enter event
  • 200 x 200 presence message being subscribed to for that enter event i.e. 40,000 total
  • 200 presence messages being published for the leave event
  • 200 x 200 presence message being subscribed to leave event i.e. 40,000 total

So in the space of one minute, that could result in 80,400 messages on your account or 1,340 messages per second.  As we have per second rate limits on all accounts, you could unexpectedly hit these limits or incur unexpected message costs.

 

Presence is a powerful feature, however it is important to understand the n-squared pattern it follows when you have both subscribers and members present on a channel.  

 

For strategies to help minimised the number of presence messages generated, we recommend you read this article on implementing presence, which also covers alternative ways to approach presence requirements.