Chat / JavaScript
Chat JavaScript v4
Chat JavaScript
Chat
JavaScript
Home
/
Chat
/
JavaScript
This is the new Docs for Chat SDK v4 for JavaScript. To see the previous Docs, click here.

Send your first message

With Sendbird Chat SDK for JavaScript, you can efficiently integrate real-time chat into a client app. On the client-side implementation, you can initialize and configure the chat with minimal effort. On the server side, Sendbird ensures reliable infra-management services for the chat service within your app.

This page demonstrates how to install the Chat SDK in your app so that you can send your first message in just a few simple steps. In addition, try building your app on top of our sample app to jumpstart your build.

Note: To learn about how to build a chat app using React Native and Hooks, see our React Native chat tutorial.


Supported browsers

The following table lists browsers and their versions we support.

BrowserVersions

Edge

13 or higher

Chrome

16 or higher

Firefox

11 or higher

Safari

7 or higher

Opera

12.1 or higher

iOS Safari

7 or higher

Android Browser

4.4 (Kitkat) or higher

Note: The Sendbird server supports Transport Layer Security (TLS) from version 1.0 up to 1.3. For example, in the server regions where TLS 1.3 isn't available, lower versions, sequentially from 1.2 to 1.0, are supported for secure data transmission.


Before you start

Before installing Sendbird Chat SDK, you need to create a Sendbird application on Sendbird Dashboard, which comprises everything required in a chat service including users, messages, and channels. You need the Application ID of your Sendbird application from the dashboard when initializing the Chat SDK.

  1. Go to Sendbird Dashboard and create an account for a free trial. If you already have a Sendbird account, sign into your account.

  2. Create a new application by clicking Create + at the bottom right of your screen.

  3. Enter a name for your application. Choose a Product Type and Region. Then, click Confirm.

  4. Click the application you just created under Applications. You will see the application's Application ID on the overview page which you will need when initializing the Chat SDK.

Each Sendbird application can be integrated with a single client app. Within the same application, users can communicate with each other across all platforms, whether they are on mobile devices or on the web.

Sendbird UIKit

UIKit is a Sendbird Chat SDK add-on with user interfaces that enable an easy and fast integration of standard chat features into new or existing client apps.

If you would like a sample app with embedded UIs, see UIKit Overview for React.


Get started

To send a message in a client app, build and configure an in-app chat using Sendbird Chat SDK. The SDK can be installed through either a web sample project or React Native project. Sendbird Chat SDK v4 supports both TypeScript and JavaScript.

Step 1 Install Sendbird Chat SDK

Installing Sendbird Chat SDK is simple if you're familiar with using external libraries or SDKs in your projects. The Chat SDK can be installed through either npm or a script element.

Note: If you use a script tag to embed the Chat SDK, we recommend a module import to guard against a security breach.

npm

You can install the Chat SDK with npm using the following command.

$ npm install --save @sendbird/chat

The Chat SDK has type definition files that include third-party library dependencies like @react-native-async-storage/async-storage to provide accurate type support.

If you don't use such third-party libraries or prefer to not check the types of the Chat SDK when compiling, enable the skipLibCheck option in your tsconfig.json file.

// your tsconfig.json
{
  "complierOptions": {
    "skipLibCheck": true
  }
}

Script element

You can directly include the SDK in your webpage by downloading sendbird.min.js from the SDK repository. Note that sendbird.min.js doesn't take advantage of tree-shaking.

<script type="text/javascript" src="your_path_to_sdk/sendbird.min.js"></script>

The Sendbird SDK instance, which contains all exported components, is made available globally with the use of a script element. The following is an example of how to import SDK components.

var {
    SendbirdChat,
    GroupChannelModule,
} = Sendbird;

var sendbird = SendbirdChat.init({
    appId,
    modules: [
        new GroupChannelModule(),
    ],
});

Module

If you use a script tag to embed the SDK, you may be vulnerable to cross-site scripting (XSS) attacks which could open you up to further attacks such as spoofing. To better protect your app against malicious attacks, import the SDK as a module or build your app with a bundler such as webpack to not expose the SDK.

Importing the SDK as a module requires higher browser versions than those supported by our SDK. The higher browser versions prevent the module from being accessed globally. Visit this page to see which browser versions support modules.

The following is an example of a module import.

<!-- your_webpage.html -->
<script type="module" src="your_app.js"></script>
// your_app.js
import SendbirdChat from 'your_path_to_sdk/sendbird.js';
import { GroupChannelModule } from 'your_path_to_sdk/groupChannel.js';

const sendbird = SendbirdChat.init({
    appId,
    modules: [
        new GroupChannelModule(),
    ],
});

Step 2 Initialize Sendbird Chat SDK

Now, initialize Sendbird Chat SDK in the app to allow the Chat SDK to respond to changes in the connection status in JavaScript client apps. Initialization requires your Sendbird application's Application ID, which can be found on Sendbird Dashboard.

The SendbirdChat.init() method must be called across a client web app at least once. We recommend that you initialize the Chat SDK at the top of your JavaScript file.

Depending on which chat feature you would like to use, add either OpenChannelModule or GroupChannelModule to create an open channel or a group channel respectively.

import SendbirdChat from '@sendbird/chat'
import { OpenChannelModule, SendbirdOpenChat } from '@sendbird/chat/openChannel';

const sb = SendbirdChat.init({
    appId: APP_ID,
    modules: [
        new OpenChannelModule(),
    ],
}) as SendbirdOpenChat;

Step 3 Connect to the Sendbird server

You will need a user in order to send a message to a channel. You can either create a user on our dashboard or use a unique ID that hasn't been taken by any of your Sendbird application users. In the latter case, a new user will be automatically created in your Sendbird application before being connected.

To learn about using a token to authenticate a user, go to the authentication page.

// The USER_ID below should be unique to your Sendbird application.
try {
    const user = await sb.connect(USER_ID);
    // The user is connected to the Sendbird server.
} catch (err) {
    // Handle error.
}

Step 4 Create a new open channel

Create an open channel. Open channels are where all users in your Sendbird application can easily participate without an invitation.

The following codes show how to create an open channel.

try {
    const params: OpenChannelCreateParams = { // OpenChannelCreateParams can be imported from @sendbird/chat/openChannel.
        // ...
    };
    const channel = await sb.openChannel.createChannel(params);

    // An open channel is successfully created.
    // Through the openChannel parameter of the callback function,
    // you can get the open channel's data from the result object that the Sendbird server has passed to the callback function.
    // ...
} catch (err) {
    // Handle error.
}

You can also create a group channel to send a message. To learn more, see the create a channel page.

Step 5 Enter the channel

Enter the open channel to send and receive messages.

await channel.enter();
// The current user successfully enters the open channel,
// and can chat with other users in the channel by using APIs.
// ...

Step 6 Send a message to the channel

Finally, send a message to the channel. To learn more about the message type you can send, see the messages page.

const params: UserMessageCreateParams = { // UserMessageCreateParams can be imported from @sendbird/chat/message.
    message: TEXT_MESSAGE,
};

channel.sendUserMessage(params)
    .onFailed((err: Error, message: UserMessage) => {
    // Handle error.
    })
    .onSucceeded((message: UserMessage) => {
        // The message is successfully sent to the channel.
        // The current user can receive messages from other users through the onMessageReceived() method of an event handler.
        // ...
    });

Step 7 Receive a message

Add the onMessageReceived() channel event handler using the addGroupChannelHandler() or addOpenChannelHandler() method so that you can receive the message you just sent to the channel. You can also see the message on our dashboard.

const channelHandler = new OpenChannelHandler();    // OpenChannelHandler can be imported from @sendbird/chat/openChannel.
channelHandler.onMessageReceived = (channel: BaseChannel, message: BaseMessage) {
    // ...
};

sb.openChannel.addOpenChannelHandler(UNIQUE_HANDLER_ID, channelHandler);
// The UNIQUE_HANDLER_ID argument should be a unique identifier
// to register multiple concurrent handlers.