2018-04-13 02:04:51 +02:00
|
|
|
# Presence
|
|
|
|
|
|
|
|
This document explains the model for Zulip's presence.
|
|
|
|
|
|
|
|
In a chat tool like Zulip, users expect to see the “presence” status
|
|
|
|
of other users: is the person I want to talk to currently online? If
|
|
|
|
not, were they last online 5 minutes ago, or more like an hour ago, or
|
2021-08-20 21:53:28 +02:00
|
|
|
a week? Presence helps set expectations for whether someone is likely
|
|
|
|
to respond soon. To a user, this feature can seem like a simple thing
|
|
|
|
that should be easy. But presence is actually one of the hardest
|
2018-04-13 02:04:51 +02:00
|
|
|
scalability problems for a team chat tool like Zulip.
|
|
|
|
|
|
|
|
There's a lot of performance-related details in the backend and
|
2021-08-20 21:53:28 +02:00
|
|
|
network protocol design that we won't get into here. The focus of
|
2018-04-13 02:04:51 +02:00
|
|
|
this is what one needs to know to correctly implement a Zulip client's
|
2021-05-14 00:16:30 +02:00
|
|
|
presence implementation (e.g. web app, mobile app, terminal client, or
|
2018-04-13 02:04:51 +02:00
|
|
|
other tool that's intended to represent whether a user is online and
|
|
|
|
using Zulip).
|
|
|
|
|
|
|
|
A client should report to the server every minute a `POST` request to
|
2021-08-20 21:53:28 +02:00
|
|
|
`/users/me/presence`, containing the current user's status. The
|
|
|
|
requests contains a few parameters. The most important is "status",
|
2018-04-13 02:04:51 +02:00
|
|
|
which had 2 valid values:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- "active" -- this means the user has interacted with the client
|
2022-01-27 22:22:59 +01:00
|
|
|
recently.
|
2021-08-20 21:45:39 +02:00
|
|
|
- "idle" -- the user has not interacted with the client recently.
|
2018-04-13 02:04:51 +02:00
|
|
|
This is important for the case where a user left a Zulip tab open on
|
2022-01-27 22:22:59 +01:00
|
|
|
their desktop at work and went home for the weekend.
|
2018-04-13 02:04:51 +02:00
|
|
|
|
|
|
|
The client receives in the response to that request a data set that,
|
|
|
|
for each user, contains their status and timestamp that we last heard
|
2021-08-20 21:53:28 +02:00
|
|
|
from that client. There are a few important details to understand
|
2018-04-13 02:04:51 +02:00
|
|
|
about that data structure:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- It's really important that the timestamp is the last time we heard
|
2021-08-20 21:53:28 +02:00
|
|
|
from the client. A client can only interpret the status to display
|
2018-04-13 02:04:51 +02:00
|
|
|
about another user by doing a simple computation using the (status,
|
2021-08-20 21:53:28 +02:00
|
|
|
timestamp) pair. E.g. a user who last used Zulip 1 week ago will
|
|
|
|
have a timestamp of 1 week ago and a status of "active". Why?
|
|
|
|
Because this correctly handles the race conditions. For example, if
|
2020-03-17 13:57:10 +01:00
|
|
|
the threshold for displaying a user as "offline" was 5 minutes
|
2018-04-13 02:04:51 +02:00
|
|
|
since the user was last online, the client can at any time
|
|
|
|
accurately compute whether that user is offline (even if the last
|
|
|
|
data from the server was 45 seconds ago, and the user was last
|
|
|
|
online 4:30 before the client received that server data).
|
2021-08-20 21:45:39 +02:00
|
|
|
- Users can disable their own presence updates in user settings
|
2021-02-27 14:21:59 +01:00
|
|
|
(`UserProfile.presence_enabled` is the flag storing [this user
|
|
|
|
preference](https://zulip.com/help/status-and-availability#disable-updating-availability)).
|
2021-08-20 21:45:39 +02:00
|
|
|
- The `status_from_timestamp` function in `static/js/presence.js` is
|
2018-04-13 02:04:51 +02:00
|
|
|
useful sample code; the `OFFLINE_THRESHOLD_SECS` check is critical
|
|
|
|
to correct output.
|
2021-08-20 21:45:39 +02:00
|
|
|
- We provide the data for e.g. whether the user was online on their
|
2018-04-13 02:04:51 +02:00
|
|
|
desktop or the mobile app, but for a basic client, you will likely
|
|
|
|
only want to parse the "aggregated" key, which shows the summary
|
|
|
|
answer for "is this user online".
|