Initialization & Users

Last Edit: Aug 08 2020

The code below creates a chat client instance for browser/mobile usage. Additional options, such as API base URL and request timeouts, can be provided to the client.Setting.

const client = new StreamChat('YOUR_API_KEY', {
    timeout: 6000,

// Typically done in your Application class
ChatClient client = new ChatClient.Builder("{{ api_key }}", context).build();

// Static reference to initialised client
ChatClient theSameClient = ChatClient.instance();

// Import StreamChatClient framework.
import StreamChatClient

// Setup the Stream Chat Client with your API key 
// Preferably in `application(_ application:didFinishLaunchingWithOptions:)`
// This needs to be called only once, since a singleton cannot be configured multiple times!
Client.configureShared(.init(apiKey: "{{ api_key }}"))
// Note: If you want to enable logs for requests and events you can enable them with extra parameter:  `logOptions`.

// Typically done in your Application class
val client = ChatClient.Builder("{{ api_key }}", context).build()

// Static reference to initialised client
val client = ChatClient.instance()

final client = Client(
  logLevel: Level.INFO,
  connectTimeout: Duration(milliseconds: 6000),
  receiveTimeout: Duration(milliseconds: 6000),

Setting the User

Once initialized, you must specify the current user with setUser:

await client.setUser(
        id: 'john',
        name: 'John Doe',
        image: '',

// this hashmap allows you to add any custom fields you want to store about your user
// the UI components will pick up name and image by default

HashMap<string, object=""> extraData = new HashMap<>();
extraData.put("name", "Bender");
extraData.put("image", "");

User user = new User(USER_ID, extraData);
client.setUser(user, "FEED_USER_TOKEN", new ClientConnectionCallback() {
    public void onSuccess(User user) {
        Log.i(TAG, String.format("Connection established for user %s", user.getName()));

    public void onError(String errMsg, int errCode) {
        Log.e(TAG, String.format("Failed to establish websocket connection: %s", errMsg));

// Create a user when they login
let userExtraData = UserExtraData(name: "Bender", avatarURL: URL(string: "")!)
let user = User(id: "bender", extraData: userExtraData)

// You can setup a user token in 2 ways.
// 1. Setup the current user with a JWT token.
Client.shared.set(user: user, token: token) { connection in
  // handle completion

// 2. Setup the current user with a token provider closure.
let tokenProvider: TokenProvider = { supplyToken in
    // Make a request here to your backend to generate a valid token for the user.
    YourTokenService.shared.getToken(for: user) { token
Client.shared.set(user: user, tokenProvider: tokenProvider) { result in 
  // handle result

User user = new User("user-id");
String token = "{{ chat_user_token }}";

// extraData allows you to add any custom fields you want to store about your user
user.getExtraData().put("name", "Bender");
user.getExtraData().put("image", "");

client.setUser(user, token, new InitConnectionListener() {
    public void onSuccess(@NotNull ConnectionData data) {
        User user = data.getUser();
        String connectionId = data.getConnectionId();

    public void onError(@NotNull ChatError error) {

final user = User(id: "john", extraData: {
  "name": "John Doe",
  "image": "<a href="" target="_self"></a>",

await client.setUser(user, "{{ chat_user_token }}");

Note how we are waiting for the setUser API call to be completed before moving forward. You should always make sure to have the user set before making any more calls. All SDKs make this very easy and wait or queue requests until then.

Set User Parameters

Name Type Description Default Optional
user object The user object. Must have id field. It can have as many custom fields as you want, as long as the total size of the object is less than 5KB
userToken string The user authentication token. See next sections for details default

User Fields

The id field is the only required field for the user. There are a few other fields you should know about though:

Name Type Description Default Optional
id string The id field is required
name string The name of the user used by our component libraries. This is just a convention and not a required field.
image string The image for this user. This is used by our component libraries but other than that is not required.
invisible boolean Determines if the user should show as offline even when they are online (only visible on the user info).
banned boolean True when the user was banned from all channels
last_active string Reserved field indicating when the user was last active.
online boolean Reserved field indicating if the user is currently online.
role string Reserved field indicating the user's role. Can be either admin, user or guest.
mutes array Reserved field containing a list of mutes by this user (only visible on the user info).
created_at date Reserved field indicating when the user was created.
updated_at date Reserved field indicating when the user was last updated.