User Presence

You can programmatically check the online status and current activity of users on your app.

POST/v1/{appId}/presences

Getting a list of online users

This lists all currently online users in your TalkJS application.

The response has a data field with an map of User objects with only a widgets field.

POST/v1/{appId}/presences
1{
2 includeBackgroundSessions?: boolean,
3 selectedConversationId?: string | null,
4 hasFocus?: boolean,
5 isTyping?: boolean,
6 userIds?: string[]
7}

About background and widget sessions

If you create a TalkJS session on every page, which we recommend, then we internally store one background session for this user. In addition we also record one session for each visible TalkJS UI widget (an Inbox, a Chatbox or a popup widget). By default we do not return users with only a background session. You can change this behaviour by passing the includeBackgroundSessions option.

Available options

selectedConversationId
(optional)string

Filter users online in the given conversation. Requires passing in a conversationID. You can pass an explicit null value to filter users who are online but have no conversation selected.

Omit this field to not filter on currently selected conversations.

hasFocus
(optional)boolean

When set to true: only return users who are currently watching the TalkJS UI. When set to false: only return users who are not watching the TalkJS UI. Omit this field to get all users regardless of hasFocus.

isTyping
(optional)boolean

Only return users who are currently typing in a conversation. Omit this field to get all users regardless of isTyping.

includeBackgroundSessions
(optional)boolean

Controls the filtering based on visibility of sessions. The following values are accepted:

  • false (default): Only return users who have a visible TalkJS UI widget.
  • true: Return all online users. Users without a visible TalkJS UI widget will be returned with an empty widgets field.
userIds
(optional)string[]

Only show sessions for the given users. This is limited to a maximum of 1000 per request.

Omit this option to not filter on userIds.

Examples

Get all users from conversation 987654

POST/v1/{appId}/presences
1{
2 selectedConversationId: "987654"
3}

Get sessions for users 1234, 5678 and 9012

POST/v1/{appId}/presences
1{
2 userIds: ["1234", "5678", "9012"]
3}

Get sessions for users that are typing in conversation 987654

POST/v1/{appId}/presences
1{
2 selectedConversationId: "987654",
3 isTyping: true
4}

List users who are online but not in any conversation right now.

POST/v1/{appId}/presences
1{
2 selectedConversationId: null
3}

Deprecated Legacy presence API

{userId} is your internal unique identifier for the user.

GET/v1/{appId}/users/{userId}/sessions
1[
2 {
3 "isTyping": boolean,
4 "currentConversationId": string | null
5 },
6 ...
7]

If you create a TalkJS session on every page, which we recommend, then you may see multiple session objects in the response: one for the TalkJS background session, and one for each visible TalkJS UI (an Inbox, a Chatbox or a popup widget).

Examples

User 6789 is offline:

1// request:
2GET https://api.talkjs.com/v1/YOUR_APP_ID/users/6789/sessions
3
4// response:
5[]

User 6789 is logged into your site, but not looking at a TalkJS UI:

1// request:
2GET https://api.talkjs.com/v1/YOUR_APP_ID/users/6789/sessions
3
4// response:
5[
6 {
7 "isTyping": false,
8 "currentConversationId": null
9 }
10]

User 6789 is logged into your site and looking at conversation 1234 in the Inbox, currently busy typing a message:

1// request:
2GET https://api.talkjs.com/v1/YOUR_APP_ID/users/6789/sessions
3
4// response:
5[
6 {
7 "isTyping": false,
8 "currentConversationId": null
9 },
10 {
11 "isTyping": true,
12 "currentConversationId": "1234"
13 }
14]