Configuring the application

Let's dive deeper into Sinch Chat SDK and have a closer look at some of its fundamental aspects.

Initialize the SDK

Once the script has loaded correctly, the initialize method should be called. It performs some internal bootstrapping logic.

Copy
Copied
await SinchSdk.initialize()

The method returns a promise that will resolve when the SDK is successfully initialized.

Set Identity

Now that the SDK is ready, we have to establish user's identity for the session. First argument passed to setIdentity method has to be a configuration declaring clientId, projectId and region. The second argument is optional, depending on the type of identity you want to choose for your users. There are two types: anonymous and signed.

Anonymous identity

If you want anonymous session all you have to do is pass the clientId, projectId and region. This generates a random identity for the users. The identity will later be stored in the browser's localStorage to be persisted after the page is closed.

Copy
Copied
await SinchSdk.setIdentity({ clientId, projectId, region })

Signed identity

If you want to provide your own user's identity, set your uuid and uuidHash.

Copy
Copied
await SinchSdk.setIdentity({ clientId, projectId, region }, { uuid, uuidHash })
Note:

Why uuidHash? If we relied only on the provided identity, anyone with a few programming skills could misuse that to set set any identity, for instance, to impersonate another user. That's why we recommend the uuidHash to be calculated on back-end side and provided to front-end for the very user which identity we previously verified. With that, the user will only have its own uuidHash and won't be able to impersonate someone else!

After successful authorization, the token will be saved in local storage.

Calculating uuidHash

To get uuidHash, you'll need the following: your Sinch Chat client secret, the original uuid, and a hashing algorithm (we use HMACSHA512). Here are some example implementations:

Node.jsJavaC#Python
Copy
Copied
var crypto = require("crypto");

function getUuidHash(uuid, secret) {
    var hmac = crypto.createHmac("sha512", secret);
    var signed = hmac.update(new Buffer(uuid, 'utf-8')).digest("hex");
    return signed
}
Copy
Copied
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;

public class Main {
    public static String getUuidHash(String uuid, String secret) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA512");
        Mac hmacSha512 = Mac.getInstance("HmacSHA512");
        hmacSha512.init(secretKeySpec);
        byte[] hashBytes = hmacSha512.doFinal(uuid.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(hashBytes);
    }

    public static void main(String[] args) throws Exception {
        String uuid = "your-uuid-here";
        String secret = "your-secret-key-here";
        String hash = getUuidHash(uuid, secret);
        System.out.println(hash);
    }
}
Copy
Copied
using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static string GetUuidHash(string uuid, string secret)
    {
        using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(secret)))
        {
            byte[] uuidBytes = Encoding.UTF8.GetBytes(uuid);
            byte[] hashBytes = hmac.ComputeHash(uuidBytes);
            return Convert.ToBase64String(hashBytes);
        }
    }

    static void Main(string[] args)
    {
        string uuid = "your-uuid-here";
        string secret = "your-secret-key-here";
        string hash = GetUuidHash(uuid, secret);
        Console.WriteLine(hash);
    }
}
Copy
Copied
import hmac
import hashlib
import base64

def get_uuid_hash(uuid, secret):
    secret_bytes = secret.encode('utf-8')
    uuid_bytes = uuid.encode('utf-8')
    hmac_sha512 = hmac.new(secret_bytes, uuid_bytes, hashlib.sha512)
    hash_bytes = hmac_sha512.digest()
    return base64.b64encode(hash_bytes).decode('utf-8')

Configuration parameters

To connect your Sinch Chat client to your Sinch account, you need to set the following parameters:

Parameter Required Type Description
projectId Yes string Get your project ID from your Sinch Dashboard.
clientId Yes string Get your client ID from your Sinch Dashboard.
region Yes string Use either EU for Europe or US for the United States.
uuid No string The application's internal user ID.
uuidHash No string The generated hash for the signed token.
We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.