Session

This component represents a user's active TalkJS session. It handles authenticating your app with TalkJS. You need to ensure that your users can only have a TalkJS session when they have a valid session in your app.

A Session component can be used standalone or with Chatbox as its descendant. If you need to use a Chatbox then it MUST be a descendant of Session. It, however, does not need to be a direct descendant.

Props

NameTypeDescription
appId Required
stringYour app's unique TalkJS ID. Get it from the Settings page of your dashboard.
me Required
UserA User object that identifies the currently active user. The user is uniquely identified by their id.
enablePushNotifications
booleanThis determines whether push notifications will be sent to the device the user is currently using.

If not passed or explicitly set to undefined, the SDK will not automatically register or unregister the current device. You'll have to call setPushRegistration or unsetPushRegistration accordingly.
signature
stringThis is the HMAC-SHA256 hash of the current user id, signed with your TalkJS secret key. It is used as part of Identity Verification
onMessage
MessageHandlerTriggered when a message is sent in a conversation the current user is in.
onUnreadsChange
UnreadsChangeHandlerTriggered when the list of unread conversations changes.

Methods

hasValidCredentials

Verifies whether the appId is valid.

Returns a Promise of a boolean that never rejects.

1hasValidCredentials(): Promise<boolean>

Returns

Promise<boolean>

clearPushRegistrations

Unregisters all the push registration device tokens for the currently active user.

1clearPushRegistrations(): void

Returns

void

setPushRegistration

Registers the given device token with the TalkJS backend to receive push notifications from the given platform.

1setPushRegistration(token: { provider: 'fcm'| 'apns', pushRegistrationId: string }): void

Parameters

NameTypeDescription
token RequiredDeviceTokenObject detailing the push notification platform and device id to be registered

Returns

void

unsetPushRegistration

Unregisters the given device token from the TalkJS backend.

1unsetPushRegistration(token: { provider: 'fcm' | 'apns', pushRegisrationId: string }): void

Parameters

NameTypeDescription
token RequiredDeviceTokenObject detailing the push notification platform and device id to be unregistered

Returns

void

Example

1<Session
2 appId='YOUR_APP_ID'
3 me={{
4 id: '123456789',
5 name: 'Alice',
6 photoUrl: 'https://talkjs.com/images/avatar-1.jpg',
7 role: 'default'
8 }}
9 enablePushNotifications={true}
10 signature='HMAC-SHA256_OF_USER_ID'
11/>

Type Definitions

MessageHandler

Triggered when a message is sent in a conversation the current user is in.

A message event is fired every time a message is sent or received by the current user (even if no TalkJS UI is visible).

Note: MessageHandler does not get triggered for conversations where the current user is a guest. It only applies to users who were added as participants.

1type MessageHandler = (message: Message) => void;

Parameters

NameTypeDescription
messageMessageAn object representing a TalkJS message.

UnreadsChangeHandler

Triggered when the list of unread conversations changes.

A "change" event is fired right after the Session loads as well as every time the amount of unread conversations changed. The handler is called with an array of objects with limited information about each conversation.

1type UnreadsChangeHandler = (unreadConversations: UnreadConversation[]) => void;

UnreadConversation

Properties

FieldTypeDescription
conversationConversationDataThe ConversationData of the unread conversation.
lastMessageMessageContains the last Message for this conversation.
unreadMessageCountnumberThe number of unread messages in this conversation

DeviceToken

Contains details regarding the device's push registration ID and the platform the ID is to be used for.

1{
2 provider: 'fcm',
3 pushRegistrationId: 'dIGCiO8vTiaXOEIwGYepEC:APA91bEzZR9Yn1PY0oWiYFUsCmoKuK5xH51b8ybBiQscHSfTMW3qtuBc7DydMEF05B8qRJBdgaQmCFiWhGqP5tXl7M_Lbvx01bHIf8Gsz99CFXCxGU0MFVzGUFTDJDx0oDfV_U315kkp'
4}

Properties

NameTypeDescription
provider Requiredfcm | apnsThe platform to be used to send push notifications.
pushRegistrationId RequiredstringThe push registration device id for the given device.