Chat Room JS-SDK
Overview
This project is the service logic layer SDK for Polyv's chat room. Developers can use this SDK to integrate chat room services or customize the chat interface based on this SDK.
Usage
Installation
npm i @polyv/chat-sdk
Initialization
Pass parameters to instantiate the SDK class, then call the instance method setup.
import { Chat } from '@polyv/chat-sdk';
const chat = new Chat({
// 聊天室连接授权 token 获取方式参考 https://help.polyv.net/index.html#/live/api/channel/operate/get_chat_token
token: '聊天室 token',
// 聊天室用户信息
userInfo: {
// 用户id
userId: '',
// 用户昵称
nick: '',
// 用户头像
pic: '',
// 用户身份类型(如普通观众student、讲师teacher、云课堂观众slice)
userType: 'student',
// 头衔(如"讲师")
actor: ''
},
// 聊天室频道(房间)信息
channelInfo: {
// 频道id
channelId: '',
// 房间id
roomId: '',
// 频道所属账号id
accountId: '',
// 频道当前场次id
sessionId: '',
},
// 频道API访问令牌 channelToken 更新函数。对于讲师,部分功能需要传入获取 channelToken 及 appId 的函数才能正常使用。
// channelToken 获取方式参考 https://help.polyv.net/index.html#/live/api/channel/auth/get_channel_api_access_token
getChannelToken: (callback) => {
// ... 获取 channelToken 及 appId
callback({ channelToken, appId })
},
});
chat.setup();
Updating Configuration
In certain situations, it is necessary to update the configuration information of the chat room SDK.
For example, when a live streaming session changes, to ensure that user messages in the chat room are associated with the new session, the new live session ID needs to be passed in.
Method for updating configuration (the parameter structure is consistent with the constructor parameters of the SDK class; only partial fields can be provided, and the SDK will merge and update internally).
chat.updateConfig({
channelInfo: {
sessionId: '',
}
});
Destroying an Instance
Use the destroy method to destroy the chat room SDK instance. After destruction, the WebSocket connection will be disconnected, and event listener logic will be cleared.
chat.destroy();
Common Instance Properties
| Property | Type | Description |
|---|---|---|
| events | Object | Chat room SDK event list |
| msgTypes | Object | Chat room SDK encapsulated message types |
| uploader | Object | Chat room image message upload and send utility |
Common Instance Methods
| Method | Parameters | Return Value | Description |
|---|---|---|---|
| setChatEnabled | Set global mute | Boolean | Promise |
Event Handling
Event Names
Access SDK event constants via chat.events or Chat.EVENTS to listen to and handle chat room events.
console.log(chat.events);
Listening to and Unlistening to Events
// 事件处理函数
const listener = (event) => {
console.log(event);
};
// 使用 chat.on 监听事件
chat.on(chat.events.SPEAK, listener);
// 使用 chat.off 取消监听事件
chat.off(chat.events.SPEAK, listener);
Chat Room Control
This section is for special roles such as instructors or administrators, who have permissions to manage chat room messages.
Chat Room Mute
Use the setChatEnabled method to close the chat room. After closing, regular users cannot send messages.
Note: The getChannelToken parameter must be configured for the chat room SDK to call the corresponding permission interface.
async function setChatEnabled(enabled) {
// enabled 为 Boolean,true 表示开启聊天室,false 表示关闭聊天室
await chat.setChatEnabled(enabled);
}
Other Notes
setup is an asynchronous method that returns a promise, which is resolved once the chat room connection is successful.
It is not necessary to wait for this promise to complete; you can set up and immediately listen for chat room events to handle initial events such as CONNECT.
