Emitter provides a presence service, seamlessly integrated with the entire service and transparent for the users. The presence can be used for game lobbies, chat rooms and machine-to-machine communication. For such applications it could be useful to see how many people are subscribed to a channel, or even to see which users are subscribed. For certain applications you might trigger an event if the number of users exceeds a certain value.
There are two ways of retrieving presence information, which are complementary in nature: 1. Get current subscribers: receive a status message that describes quantity of subscribers and user names of subscribers upon request. 2. Get subscription updates: Receive a change notification each time a user subscribes or unsubscribes.
At any time, you can retrieve a list of the current subscribers and the total number of subscribers of a channel by calling the presence
function in your application. You must specify a valid security key which has a presence retrieval permission (p
flag). For example, in order to request for a message to see the connections to channel chat/sports/
, we can call presence()
function as shown here:
emitter.presence({
key: "<channel key>",
channel: "chat/sports",
status: true,
changes: false
});
Different arguments of this function allow you to specify the presence information to retrieve.
p
flag) matching the channel.In the above example you would get an immediate response from the emitter broker, containing the list of users currently subscribed to the channel. An example of such response is shown below.
{
event: "status",
channel: "chat/sports",
occupancy: 2,
time: 1471771395,
who: [{
id: "Ekm4hAAAAABesHHK",
username: "user1"
},
{
id: "A4EcAAAAABesHHE",
username: "user2"
}]
}
"status"
: a response that contains a list of currently subscribed clients."subscribe"
: an event (push notification) that contains a user who has subscribed."unsubscribe"
: an event (push notification) that contains a user who has unsubscribed.client = emitter.connect({ username: "user1" });
There are many cases where retrieving a status is not enough and you might be interested receiving real-time presence notifications each time a user subscribes or unsubscribes from a channel. For example, for chat/sports/
channel shown below, we call the presence()
function with changes
flag set to true
.
emitter.presence({
key: "<channel key>",
channel: "chat/sports",
status: false,
changes: true
})
This is the same function as for getting the current presence status, except now the status
argument is set to false
and the changes
argument is set to true
. In the above example you will now get a message from the emitter broker each time a user (un)subscribes from the chat/sports/
channel.
{
event: "subscribe",
channel: "chat/sports/",
occupancy: 2,
time: 1471771395,
who: {
id: "A4EcAAAAABesHHE",
username: "user2"
}
}
"subscribe"
if a user subscribed or "unsubscribe"
if a user unsubscribes.who: user (connection id and an optional username) that have (un)subscribed.
If you want to stop receiving these push notifications, you simply call the presence()
function once again with the changes
argument set to false
:
emitter.presence({
key: "<channel key>",
channel: "chat/sports",
status: false,
changes: false
})
When you request presence for a channel, you will get presence information for the exact channel you specified. In some cases users might have subscribed to channels using wildcards or partial channels. The below example explains how presence works in combination with wildcards or partial channels. Suppose users are subscribed as follows:
Channel | Number of Users |
---|---|
chat/weather/ | 7 |
chat/sports/ | 10 |
chat/+/ | 2 |
chat/ | 1 |
This would also exactly be the occupancy (number of users) you wouldd get back if you asked for presence information to the emitter service for these channels. Please be aware that in the above example in fact 13 users receive messages from channel chat/sports/
. However, only 10 users are subscribed to the exact channel chat/sports/
. So if you would request presence information for channel chat/sports/
you would get an occupancy of 10, for channel chat/+/
you would get occupancy of 2 and so on. Also subscription updates are only provided for the exact channel you ran the presence fuction for. So depending on the use case it might be necessary to request presence information for channels chat/sports/
, chat/+/
and chat/
separately and sum up the occupancy data yourself.
That's it for the presence. Feel free to contact us if you have any particular questions and contribute to this guide if you feel that something is unclear or requires more explanations. On a final note, there are few more resources you might want to check out, including some demos and source code for a complete application that demonstrates our presence feature.