SAMA chat server API: Activities
STABLE — AVAIBLE — VERSATILE
I think everyone has had a conversation where your opponent didn’t answer for a long time, and you didn’t know whether they were busy or just ignoring you. That’s why such a small and at the same time big thing as the last user activity will be an important element in an online conversation.
📜 Online, or not online, that is the question… 🪶
Use these 3 methods from the SAMA Activities API to implement real-time user status tracking 👀:
- 🔔
status_subscribe
| Subscribe to user activity - 🔕
status_unsubscribe
| Unsubscribe from a user’s activity - 👁🗨
get_user_status
| Get the latest activity of a list of users
Subscribe to user activity
~ request name: status_subscribe
Let’s subscribe to another user’s activity. It will be valid until we unsubscribe or the user is deleted.
The request ⬆️ will look like this:
{
request: {
user_last_activity_subscribe: {
id: "64e4972ec0d3198d69c6de48"
},
id: "subscribe_request"
}
}
In response ⬇️, you will receive the current activity of this user:
{
response: {
last_activity: {
64e4972ec0d3198d69c6de48: "online" | <last_activity-time>
},
id: "subscribe_request"
}
}
❗️At the moment, you can only follow one user.
Unsubscribe from a user’s activity
~ request name: status_unsubscribe
As we have already mentioned, you can only subscribe to one user, so to unsubscribe, you need to send an empty request ⬆️:
{
request: {
user_last_activity_unsubscribe: {},
id: "unsubscribe_request"
}
}
And get a response ⬇️ about the success of the operation:
{
response: {
success: true,
id: "unsubscribe_request"
}
}
Get the latest activity of a list of users
~ request name: get_user_status
Okay, if I have a group chat, how can I get the activity of all users? Do I need to subscribe to everyone?
It will be easier for you to send a single request ⬆️, where you specify the IDs of all users (up to 🔟):
request: {
user_last_activity: {
ids: [
"64e4972ec0d3198d69c6de48",
"64e5ccc00377ba662f72eff5",
...
]
},
id: "get_user_status_request"
}
}
And get a list of all user activities:
{
response: {
last_activity: {
64e4972ec0d3198d69c6de48: 1693390374,
64e5ccc00377ba662f72eff5: "online",
...
},
id: "get_user_status_request"
}
}
Conclusion
Need to track user activity 24/7? Go for it! ☝🏻
👀 Check out more posts on our page for fantastic improvements to your app!
Try SAMA 👇🏼
To run SAMA server locally — follow https://github.com/SAMA-Communications/sama-server. Frontend app is available at https://github.com/SAMA-Communications/sama-client.
Discover, enjoy, and all feedback is welcome. We will be thankful a lot for every GitHub star, issue, or comment!