Chat SDKs Unity v3
Chat SDKs Unity
Chat SDKs
Unity
Version 3
Sendbird Chat SDK v3 for Unity is no longer supported as a new version is released. Check out our latest Chat SDK v4

Authentication

Copy link

In order to use the features of the Chat SDK in your client apps, a SendBird instance should be initiated in each client app through user authentication with Sendbird server. The instance communicates and interacts with the server using an authenticated user account, and is allowed to use the Chat SDK's features. This page explains how to authenticate your user with the server.


Initialize the Chat SDK with APP_ID

Copy link

To use our chat features, you should initialize a SendBirdClient instance by passing the APP_ID of your Sendbird application to the SendBirdClient.Init(). The SendBirdClient.Init() must be called once across your client app. Typically, initialization is implemented in the user login screen.

// Initialize SendBirdClient instance to use APIs in your app.
SendBirdClient.Init(APP_ID);

Connect to Sendbird server with a user ID

Copy link

By default, Sendbird server can authenticate a user just by a unique user ID. Then the server queries the database to check for a match upon the request for connection. If no matching user ID is found, the server creates a new user account with the user ID. The ID should be unique within a Sendbird application to be distinguished from others, such as a hashed email address or phone number in your service.

This authentication procedure is useful when you are in development or if your service doesn't require additional security.

Note: Go to the Event handler page to learn more about the usages of the Chat SDK's handlers and callbacks.

// The USER_ID below should be unique to your Sendbird application.
SendBirdClient.Connect(USER_ID, (User user, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

        // The user is connected to Sendbird server.
    ...
});

Connect to Sendbird server with a user ID and an access token

Copy link

Using Chat Platform API, you can create a user along with their own access token, or issue an access token for an existing user. Once an access token is issued, a user is required to provide the access token in the SendBirdClient.connect() method which is used for signing in.

  1. Using the Chat API, create a Sendbird user account with the information submitted when a user signs up or in to your service.
  2. Save the user ID along with the issued access token to your persistent storage which is securely managed.
  3. When the user attempts to sign in to the Sendbird application, load the user ID and access token from the storage, and then pass them to the SendBirdClient.connect() method.
  4. For security, we recommend updating the user's access token periodically by issuing a new token to replace.

Note: You are able to prevent users without an access token from signing in to the application or restrict their access to read and write messages in your dashboard: Settings > Application > Security > Access Token Policy.

// The USER_ID below should be unique to your Sendbird application.
SendBirdClient.Connect(USER_ID, ACCESS_TOKEN, (User user, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    // The user is authenticated using the access token and is connected to Sendbird server.
    ...
});

Disconnect from Sendbird server

Copy link

You must disconnect from Sendbird server when your user no longer needs to receive messages from an online state. But users would receive push notifications for new messages from their group channels.

When disconnected from the server, all registered handlers and callbacks are removed. That means, it removes all event handlers added through the SendBirdClient.AddChannelHandler() or SendBirdClient.AddConnectionHandler(). It also flushes all internally cached data, such as the cached channels from calling the OpenChannel.GetChannel() or GroupChannel.GetChannel() method.

Note: By default, most of the data related to users, channels, and messages are internally cached in the SendBird instance of a user's client app, which are retrieved by the corresponding query instances or received through the event handlers.

SendBirdClient.Disconnect(() =>
{
    @Override
    public void onDisconnected()
    {
        // The current user is disconnected from Sendbird server.
        ...
    }
});

Update user profile

Copy link

Using the updateCurrentUserInfo() method, you can update a user's nickname and profile image, as well as their profile picture with a URL.

SendBirdClient.UpdateCurrentUserInfo(NICKNAME, PROFILE_URL, (SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    // The current user's profile is successfully updated.
    // You could redraw the profile in a view in response to this operation.
    ...
});

Or, you can upload an image directly using the UpdateCurrentUserInfoWithProfileImage() method.

SendBirdClient.UpdateCurrentUserInfoWithProfileImage(NICKNAME, FILE_PATH, (SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    // A new profile images is successfully uploaded to Sendbird server.
    // You could redraw the profile in a view in response to this operation.
    ...
});