Session
A session represents a TalkJS session. It also authenticates your app with TalkJS.
It is also responsible for creating User and Conversation objects.
Note: After creating the Session object,
you must set its me
property before doing anything else related to TalkJS.
Constructor | Creates a TalkJS Session. |
destroy | Invalidates this session. |
getUser | A method used to either fetch or create a user. |
getUserById | A method used to fetch an existing user. |
getConversation | A method used to either fetch or create a conversation. |
hasValidCredentials | Verifies whether the `appId` is valid. |
setPushRegistration | A method used to register push notification device tokens on the TalkJS backend. |
unsetPushRegistration | A method used to unregister push notification device tokens on the TalkJS backend. |
clearPushRegistrations | A method used to unregister all push notification device tokens for the current user. |
appId
: StringYour TalkJS AppId
that can be found on the Settings page of your TalkJS dashboard.
enablePushNotifications
: boolThis determines whether the device the user is currently using will be registered by the SDK to receive push notifications.
me
: UserA User object that identifies the currently active user.
Note: A User object is created by calling either the getUser or getUserById methods of the Session object.
Note: After creating the Session object,
you must set its me
property before doing anything else related to TalkJS.
token
: String?A token to authenticate the session with.
Pass token
if you use standard (non-refreshable) authentication.
tokenFetcher
: TokenFetcherHandler?Triggered when TalkJS needs a new authentication token.
The handler should fetch a new token from your server and return it.
Pass tokenFetcher
if you use authentication with refreshable tokens.
signature
: String?The HMAC-SHA256 hash of the current user id, signed with your TalkJS secret key.
Pass signature
if you use legacy signature-based authentication.
onMessage
: MessageHandler?Triggered when a message is sent in a conversation the current user is in.
Deprecatedunreads
: Unreads?This property is deprecated. Use onUnreadsChange
instead.
This object can notify you when the amount of unread conversations changes.
onUnreadsChange
: UnreadsChangeHandler?Triggered when the list of unread conversations changes.
1Session({2 required String appId,3 bool? enablePushNotifications = false,4 String? token,5 TokenFetcherHandler? tokenFetcher,6 String? signature,7 MessageHandler? onMessage,8 Unreads? unreads,9 UnreadsChangeHandler? onUnreadsChange,10});
Creates a TalkJS Session.
Parameters
appId
: StringYour app's unique TalkJS ID. Get it from the Settings page of your dashboard.
enablePushNotifications
(optional): bool?This determines whether the device the user is currently using will be registered to receive push notifications.
If explicitly passing null
, the SDK will not automatically register/unregister the current device. You'll have to
call setPushRegistration
and unsetPushRegistration
accordingly.
token
: String?A token to authenticate the session with.
Pass token
if you use standard (non-refreshable) authentication.
tokenFetcher
: TokenFetcherHandler?Triggered when TalkJS needs a new authentication token.
The handler should fetch a new token from your server and return it.
Pass tokenFetcher
if you use authentication with refreshable tokens.
signature
: String?The HMAC-SHA256 hash of the current user id, signed with your TalkJS secret key.
Pass signature
if you use legacy signature-based authentication.
onMessage
(optional): MessageHandler?Triggered when a message is sent in a conversation the current user is in.
Deprecatedunreads
(optional): Unreads?This property is deprecated. Use onUnreadsChange
instead.
This object can notify you when the amount of unread conversations changes.
onUnreadsChange
(optional): UnreadsChangeHandler?Triggered when the list of unread conversations changes.
1Future<void> destroy();
Invalidates this session.
You cannot use any objects that were created in this session after you destroy it.
If you want to use TalkJS after having called destroy()
you must instantiate a new Session instance.
Returns
The return value is a Future that can be awaited to be sure that the session has actually been invalidated.
1User getUser({2 required String id,3 required String name,4 List<String>? email,5 List<String>? phone,6 String? availabilityText,7 String? locale,8 String? photoUrl,9 String? role,10 Map<String, String?>? custom,11 String? welcomeMessage,12});
Fetches or creates a User.
Use this method to synchronize user data with the TalkJS backend.
The fields id
and name
are required.
A warning will be emitted if role
is not specified.
Set email
to null
if you want to use TalkJS without email fallback.
Parameters
availabilityText
(optional): String?Availability acts similarly to User.welcomeMessage but appears as a system message.
custom
(optional): Map<String, String?>?Allows you to set custom metadata for the User
One or more email address belonging to the User. The email addresses will be used for Email Notifications if they are enabled.
id
: StringThe unique ID which is used to identify the user in TalkJS
locale
(optional): String?The locale field expects an IETF language tag. See the localization documentation.
name
: StringThe User's name which will be displayed on the TalkJS UI
phone
(optional): List<String>?One or more phone numbers belonging to the User. The phone number will be used for SMS Notifications (this feature requires standard plan and up).
photoUrl
(optional): String?An optional URL to a photo which will be displayed as the user's avatar
role
(optional): String?TalkJS supports multiple sets of settings, called "roles". These allow you to change the behavior of TalkJS for different users. You have full control over which user gets which configuration.
welcomeMessage
(optional): String?The default message a user sees when starting a chat with that person
Returns
1User getUserById(String id);
Fetches an existing User.
Only use this method if you're sure that a user by the given id
already
exists in TalkJS (for instance, because you synchronized it via the REST API).
Otherwise use the getUser method instead.
Parameters
id
: StringThe unique ID which is used to identify the user in TalkJS
Returns
1Conversation getConversation({2 required String id,3 Map<String, String?>? custom,4 List<String>? welcomeMessages,5 String? photoUrl,6 String? subject,7 Set<Participant> participants = const <Participant>{},8});
Returns a Conversation
object that encapsulates a conversation between
me
(set as a property of the Session) and zero or more other
participants.
The returned Conversation
is a reference to a conversation with the given id
, that is automatically
created or updated with the given fields the first time it is used in a
ChatBox.
If conversation synchronization
is disabled, pass only the id
parameter, and make sure that a conversation
with that id
already exists.
Parameters
id
: StringA unique identifier for this conversation, such as a channel name or topic ID. Any user with access to this ID can join this conversation. Read about how to choose a good conversation ID for your use case If you want to make a simple one-on-one conversation, consider using oneOnOneId to generate one.
custom
(optional): Map<String, String?>?Custom metadata that is stored with the conversation
photoUrl
(optional): String?The URL of a photo to be used for this conversation in the TalkJS UI in case there are more than 2 participants (TalkJS shows the photo of the other participant in a 1-on-1 conversation)
subject
(optional): String?A human-readable subject of the conversation.
Supports formatted links in a Markdown-style syntax, e.g.
Beautiful <https://example.com/booking/18644|home by the sea>;!
.
URLs and email addresses are made clickable, and emojis made to work cross-platform.
welcomeMessages
(optional): List<String>?Messages which are sent at the beginning of a chat. In this case the messages will appear as system messages.
participants
(optional): Set<Participant>A set containing the participants for this conversation.
Returns
1Future<bool> hasValidCredentials();
Verifies whether the appId is valid.
Returns
The return value is a Future of a boolean.
1Future<void> setPushRegistration({2 Provider? provider,3 String? pushRegistrationId,4});
Registers push notifications to the TalkJS backend.
If the provider
and pushRegistrationId
parameters are not passed, it registers the device's current token on
Firebase if it's running on Android and on APNs if it's on iOS.
If passing parameters to this function, both provider
and pushRegistrationId
must not be null
Parameters
provider
(optional): Provider?Either Provider.fcm
or Provider.apns
.
pushRegistrationId
(optional): String?The FCM or APNS token ID.
Returns
The return value is a Future that can be awaited to be sure that the communication with the backend actually happened.
1Future<void> unsetPushRegistration({2 Provider? provider,3 String? pushRegistrationId,4});
Unregisters push notifications to the TalkJS backend.
If the provider
and pushRegistrationId
parameters are not passed, it unregisters the device's current token on
Firebase if it's running on Android and on APNs if it's on iOS.
If passing parameters to this function, both provider
and pushRegistrationId
must not be null
Parameters
provider
(optional): Provider?Either Provider.fcm
or Provider.apns
.
pushRegistrationId
(optional): String?The FCM or APNS token ID.
Returns
The return value is a Future that can be awaited to be sure that the communication with the backend actually happened.
1Future<void> clearPushRegistrations();
Unregisters all push notification device tokens for the current user
Returns
The return value is a Future that can be awaited to be sure that the communication with the backend actually happened.
1typedef TokenFetcherHandler = Future<String> Function();
Should return a new authentication token to use.
1typedef MessageHandler = void Function(Message message);
See Message for the callback parameter.