Getting Started

Initialization

Before using the Chat SDK beta, you will need to create a new SDK instance with your API key. Please find your account API key in Amity Social Cloud Console.
After logging in Console:
  1. 1.
    Click Settings to expand the menu.
  2. 2.
    Select Security.
  3. 3.
    In the Security page, you can find the API key in the Keys section.
API key in Security page
If you have trouble finding this, you can post in our community forum at community.amity.co so our support team can assist you.
1
import {
2
createClient, enableCache
3
} from "@amityco/ts-sdk";
4
5
const apiKey = "b3bee858328ef4344a308e4a5a091688d05fdee2be353a2b";
6
const apiEndpoint = "https://api.amity.co";
7
8
export const client = createClient(apiKey, apiEndpoint)
9
enableCache()
Copied!
You can specify apiEndpoint manually via optional parameters. We currently support multi-data center capabilities for the following regions:
Region
Endpoint
Europe
https://api.eu.amity.co
Singapore
https://api.sg.amity.co
United States
https://api.us.amity.co

Authentication

In order to use any Chat SDK features, you must first register the current device with a userId. A registered device will be tied to the registered userId until the device is either proactively unregistered, or until the device has been inactive for over 90 days. A registered device will receive all the events messages belonging to the tied user.
An optional displayName can be provided, which will be used in standard push notifications (related to user's actions, such as when the new message is sent).
1
import { connectClient } from '@amityco/ts-sdk';
2
const handleConnect = async (userId: string, displayName: string) => {
3
const userId = 'userId1';
4
const displayName = 'Bob Newton';
5
await connectClient(userId, displayName);
6
};
7
handleConnect();
Copied!
An authToken can be provided, which will be used for authentication. Should you choose to use insecure settings on your network, this parameter can be ignored.
On the client side, you will need to receive that authToken from before, and use it in the registerSession call. For example,
1
import { connectClient } from '@amityco/ts-sdk';
2
const handleConnect = async (userId: string, displayName: string) => {
3
const userId = 'userId1';
4
const displayName = 'Bob Newton';
5
const authToken = await getAuthToken(); // call your backend here
6
await connectClient(userId, displayName, authToken);
7
};
8
handleConnect();
Copied!
The displayName is set only on the first time the device is registered, please follow your platforms necessary directions if you would like to rename this to something else.

Unregister

When the user logs out, you should explicitly unregister the user from the SDK as well. This prevents the current device from receiving unnecessary and/or restricted data.
1
import { disconnectClient } from '@amityco/ts-sdk';
2
const handleDisconnect = async () => {
3
await disconnectClient()
4
};
5
handleDisconnect();
Copied!

Devices

Each user can be registered, at the same time, to an unlimited number of devices. Amity's Chat SDK will automatically synchronize the user data across all registered devices. We will also automatically unregister any device that has not been connected to the server for more than 90 days.
When a device is unregistered due to inactivity, the SDK data on the device will be reset. You will need to re-register this device in order to connect to the server again.

Connection Status

The user may want to know the exact network status to determine if their actions will be performed in real-time, therefore this status is exposed.
1
import {
2
isConnected,
3
onClientDisconnected,
4
} from '@amityco/ts-sdk';
5
6
const [connected, setConnected] = useState(isConnected());
7
8
onClientDisconnected(reason => {
9
setConnected(false);
10
});
Copied!

Amity Social Cloud Developer Kits

Check out our Amity Social Cloud UI Kits and Template Apps.
  • UI Kits Our UI Kits include user interfaces to enable fast integration of standard Amity Chat and Amity Social features into new or existing applications.
  • Template Apps Our Template Apps are ready made template applications to kickstart your own Amity Social Cloud project.
With real life use-cases, we guide you through ways you can get started with building stellar applications for yourself and your clients and their users
Last modified 1mo ago