Sendbird Chat SDK v3 for JavaScript
The Sendbird Chat SDK v3 for JavaScript (current stable v3.1.33) facilitates the integration of real-time chat functionalities into client applications, abstracting the complexities of backend infrastructure. It enables core chat features such as user authentication, channel management (open and group channels), message sending and receiving, and event handling. This version is officially deprecated, with v3.1.33 being its final release in August 2023. Sendbird is actively transitioning users to its v4 SDK, which was first released in April 2022 and offers an extended feature set including enhanced WebSocket handling, improved local caching, polls, scheduled messages, and pinned messages. Developers are strongly encouraged to migrate to v4 due to the end of support and future bug fixes for v3.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'localStorage')
cause The Sendbird SDK v3.1.31 or earlier attempted to access `localStorage` in a non-browser environment like React Native, where it is not available.fixUpgrade the `sendbird` package to v3.1.32 or later to resolve the `localStorage` access issue in React Native. -
Uncaught (in promise) TypeError: Cannot read property 'finally' of undefined
cause The Sendbird SDK v3 was used in an environment (e.g., older browser) that lacks native `Promise.finally()` support, and no polyfill was provided.fixUpdate the `sendbird` package to v3.1.24 or higher, which bundles a `Promise.finally()` polyfill, or include a separate `Promise.finally()` polyfill in your application. -
QuotaExceededError: Failed to execute 'setItem' on 'Storage': Setting the value of 'sendbird_message_collection_key' exceeded the quota.
cause The Sendbird `MessageCollection` is attempting to store more data in `localStorage` than the browser's allocated quota allows.fixWhile v3.1.30 had stability improvements related to this, managing `localStorage` size is key. Consider reducing the number of messages cached, clearing old data, or migrating to SDK v4 which has more robust local caching strategies. -
Error: channel.sendFileMessage() failed in web worker context
cause In Sendbird SDK v3 versions prior to v3.1.28, calling `channel.sendFileMessage()` within a Web Worker environment could lead to failures.fixUpgrade the `sendbird` package to v3.1.28 or later. If unable to upgrade, consider performing file message sending operations outside of Web Workers.
Warnings
- breaking Sendbird Chat SDK v3 is officially deprecated as of July 2023, with v3.1.33 being its final release in August 2023. No further updates, bug fixes, or security patches will be provided for this version. Users are strongly advised to migrate to Sendbird Chat SDK v4.
- breaking Migration from Sendbird Chat SDK v3 to v4 involves significant breaking changes. The API surface, initialization patterns, and underlying architecture have evolved. Direct drop-in replacement is not possible.
- gotcha Older versions of Sendbird SDK v3 (prior to v3.1.32) could cause `sb.connect()` to fail in React Native environments due to an unexpected attempt to access the browser's `localStorage` API, which is unavailable in React Native.
- gotcha When `localCacheEnabled` was set to `false`, `MessageCollection` in SDK versions prior to v3.1.31 would incorrectly call `onMessagesUpdated()` with old messages upon connection or reconnection, leading to stale UI data.
- gotcha Applications targeting older browsers or environments that do not natively support `Promise.finally()` (e.g., Internet Explorer 11) would encounter runtime errors if using Sendbird SDK v3 versions prior to v3.1.24, as the SDK lacked an internal polyfill.
Install
-
npm install sendbird -
yarn add sendbird -
pnpm add sendbird
Imports
- SendBird
import { SendBird } from 'sendbird'import SendBird from 'sendbird'
- UserMessageParams
import type { UserMessageParams } from 'sendbird' - GroupChannel
const SendBird = require('sendbird'); const channel = new SendBird.GroupChannel();import { GroupChannel } from 'sendbird'
Quickstart
import SendBird from 'sendbird';
const APP_ID = process.env.SENDBIRD_APP_ID ?? 'YOUR_SENDBIRD_APP_ID';
const USER_ID = process.env.SENDBIRD_USER_ID ?? 'testuser123';
const NICKNAME = process.env.SENDBIRD_USER_NICKNAME ?? 'Test User';
const sb = new SendBird({
appId: APP_ID,
modules: [new SendBird.ChannelModule(), new SendBird.MessageModule()]
});
async function initializeAndConnect() {
try {
console.log('Connecting to Sendbird...');
const user = await sb.connect(USER_ID);
console.log(`Connected as ${user.nickname} (${user.userId})`);
// Create or get a channel
const channelUrl = process.env.SENDBIRD_CHANNEL_URL ?? 'sendbird_open_channel_28653_373c38c116d90a78627e7f77a880949216065548';
const channel = await sb.OpenChannel.getChannel(channelUrl);
await channel.enter();
console.log(`Entered channel: ${channel.name}`);
// Send a message
const messageText = `Hello from SDK v3! Current time: ${new Date().toLocaleTimeString()}`;
const params = new sb.UserMessageParams();
params.message = messageText;
const message = await channel.sendUserMessage(params);
console.log('Message sent:', message.message);
// Disconnect (optional, for cleanup)
// await sb.disconnect();
// console.log('Disconnected from Sendbird.');
} catch (error) {
console.error('Sendbird operation failed:', error);
}
}
initializeAndConnect();