User Presence
You can programmatically check the online status and current activity of users on your app.
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.
1{2 includeBackgroundSessions?: boolean,3 selectedConversationId?: string | null,4 hasFocus?: boolean,5 isTyping?: boolean,6 userIds?: string[]7}
1{2 data:3 {userId}: {4 status: string,5 backgroundSessions: [{6 selectedConversationId: string | null,7 custom: Map<string, string>,8 hasFocus: boolean,9 isTyping: boolean,10 sessionId: string11 }],12 widgets: [{13 selectedConversationId: string | null,14 custom: Map<string, string>,15 hasFocus: boolean,16 isTyping: boolean,17 sessionId: string18 }]19 },20}
1{2 "data": {3 "623b106653a97": {4 "status": "online",5 "backgroundSessions": [],6 "widgets": [7 {8 "selectedConversationId": "987654",9 "custom": {},10 "hasFocus": true,11 "isTyping": false,12 "sessionId": "0123456789-0123456789-abcde-abcde"13 }14 ]15 }16 }17}
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.
selectedConversationId
(optional): stringFilter 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): booleanWhen 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): booleanOnly return users who are currently typing in a conversation. Omit this field to get all users regardless of isTyping.
includeBackgroundSessions
(optional): booleanControls 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 emptywidgets
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.
1{2 selectedConversationId: "987654"3}
1{2 "data":3 "623b106653a97": {4 "widgets": [5 {6 "selectedConversationId": "987654",7 "custom": {},8 "hasFocus": true,9 "isTyping": false10 }11 ]12 }13}
1{2 userIds: ["1234", "5678", "9012"]3}
1{2 "data": {3 "5678": {4 "widgets": [5 {6 "selectedConversationId": null,7 "custom": {},8 "hasFocus": false,9 "isTyping": false10 },11 {12 "selectedConversationId": "456789",13 "custom": {},14 "hasFocus": true,15 "isTyping": false16 }17 ]18 },19 "9012": {20 "widgets": [21 {22 "selectedConversationId": "987654",23 "custom": {},24 "hasFocus": true,25 "isTyping": true26 }27 ]28 }29 }30}
1{2 selectedConversationId: "987654",3 isTyping: true4}
1{2 "data": {3 "5678": {4 "widgets": [5 {6 "selectedConversationId": "987654",7 "custom": {},8 "hasFocus": true,9 "isTyping": true10 }11 ]12 },13 "9012": {14 "widgets": [15 {16 "selectedConversationId": "987654",17 "custom": {},18 "hasFocus": true,19 "isTyping": true20 }21 ]22 }23 }24}
1{2 selectedConversationId: null3}
1{2 "data": {3 "5678" : {4 "availabilityText": null,5 "createdAt": 1643719013438,6 "custom": {},7 "email": [9 ],10 "locale": null,11 "name": "Alice",12 "phone": null,13 "photoUrl": "http://localhost:50000/demo/img/alice.jpg",14 "role": "booker",15 "welcomeMessage": null,16 "widgets": [17 {18 "selectedConversationId": null,19 "custom": {},20 "hasFocus": false,21 "isTyping": false22 }23 ]24 }25 }26}
{userId}
is your internal unique identifier for the user.
1[2 {3 "isTyping": boolean,4 "currentConversationId": string | null5 },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).
1// request:2GET https://api.talkjs.com/v1/YOUR_APP_ID/users/6789/sessions34// response:5[]
1// request:2GET https://api.talkjs.com/v1/YOUR_APP_ID/users/6789/sessions34// response:5[6 {7 "isTyping": false,8 "currentConversationId": null9 }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/sessions34// response:5[6 {7 "isTyping": false,8 "currentConversationId": null9 },10 {11 "isTyping": true,12 "currentConversationId": "1234"13 }14]