Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
amazon-ivs-chat-messaging
Advanced tools
The IVS Chat Messaging SDK makes it quick and easy to build excellent chat experience in your JavaScript or TypeScript app.
Readme
The IVS Chat Messaging SDK makes it quick and easy to build excellent chat experience in your JavaScript or TypeScript app.
Before starting, you should be familiar with Getting Started with Amazon IVS Chat.
See also SDK Documentation.
$ npm install --save amazon-ivs-chat-messaging
or
$ yarn add amazon-ivs-chat-messaging
This integration requires endpoints on your server that talk to the Amazon IVS Chat API. Use the official AWS libraries for access to the Amazon IVS API from your server. These are accessible within several languages from the public packages; e.g. node.js, java, go.
Next, create a server endpoint that talks to the AWS IVS Chat CreateChatToken endpoint in order to create a chat token for chat users. You can follow our sample backend implementation as a reference.
Create a parameterless function that fetches chat token from your backend.
This will be needed to instantiate the main ChatRoom
entity in the next step.
Below you can find the sample code which uses custom client api service apiService.createChatToken
.
// ...
function tokenProvider(): Promise<ChatToken> {
return apiService.createChatToken();
}
// ...
Create an instance of the ChatRoom
class. It requires passing regionOrUrl
, which is the AWS region in which your chat room is hosted, and tokenProvider
which is the token fetching method created in the previous step.
import { ChatRoom } from 'amazon-ivs-chat-messaging';
const room = new ChatRoom({
regionOrUrl: 'us-west-2',
tokenProvider,
});
Next, you should subscribe for chat room events in order to receive lifecycle events, as well as receive messages & events delivered in the chat room.
const unsubscribeOnConnecting = room.addListener('connecting', () => {
// Called when room is establishing the initial connection or reestablishing connection after socket failure/token expiration/etc
});
const unsubscribeOnConnected = room.addListener('connect', () => {
// Called when connection has been established
});
const unsubscribeOnDisconnected = room.addListener('disconnect', () => {
// Called when a room has been disconnected
});
const unsubscribeOnMessageReceived = room.addListener('message', (message) => {
/**
* Called when chat message has been received.
*
* Example event payload: { "id": "5OPsDdX18qcJ", "sender": { "userId": "user1" }, "content": "hello world",
* "sendTime": "2022-10-11T12:46:41.723Z", "requestId": "d1b511d8-d5ed-4346-b43f-49197c6e61de" }
*/
});
const unsubscribeOnEventReceived = room.addListener('event', (event) => {
/**
* Called when chat event has been received.
*
* Example event payload: { "id": "5OPsDdX18qcJ", "eventName": "customEvent, "sendTime": "2022-10-11T12:46:41.723Z",
* "requestId": "d1b511d8-d5ed-4346-b43f-49197c6e61de" "attributes": { "Custom Attribute": "Custom Attribute Value", }
* }
*/
});
const unsubscribeOnMessageDelete = room.addListener('messageDelete', (deleteMessageEvent) => {
/**
* Called when DELETE_MESSAGE event has been received.
*
* Example event payload: { "id": "AYk6xKitV4On", "messageId": "R1BLTDN84zEO", "reason": "Spam", "sendTime":
* "2022-10-11T12:56:41.113Z", "requestId": "b379050a-2324-497b-9604-575cb5a9c5cd", "attributes": { "MessageID":
* "R1BLTDN84zEO", "Reason": "Spam" } }
*/
});
const unsubscribeOnUserDisconnect = room.addListener('userDisconnect', (disconnectUserEvent) => {
/**
* Called when DISCONNECT_USER event has been received
*
* Example event payload: { "id": "AYk6xKitV4On", "messageId": "R1BLTDN84zEO", "reason": "Spam", "sendTime":
* "2022-10-11T12:56:41.113Z", "requestId": "b379050a-2324-497b-9604-575cb5a9c5cd", "attributes": { "MessageID":
* "R1BLTDN84zEO", "Reason": "Spam" } }
*/
});
// ...
The last step of the basic initialization is connecting to the chat room by establishing the WebSocket connection. In order to do that simply call a connect()
method within the room instance.
room.connect();
The SDK will attempt to establish a connection to chat room encoded in chat token received from your server.
After calling connect()
the room will transition into connecting
state and will also emit a connecting
event. When room successfully connects, it will transition into connected
state and will emit a connect
event.
In case of connection failure, which might happen either due to issues when fetching token or issues when connecting to WebSocket, the room will try to reconnect automatically up to number of times indicated by maxReconnectAttempts
constructor parameter. During the reconnection attempts the room will be in connecting
state and will not emit additional events. After exhausting the reconnect attempts the room will transition to disconnected
state and will emit a disconnect
event with relevant disconnect reason. In disconnected
state room will no longer attempt to connect, you will need to call connect()
again to trigger the connection process.
IVS Chat Messaging SDK offers user actions dedicated for sending messages, deleting messages and disconnecting other users.
Those are available on the ChatRoom
instance, and return Promise
object that allows you to receive request confirmation or rejection.
Note: that for this particular request you need to have a
SEND_MESSAGE
capacity encoded in your chat token.
To trigger a send-message request:
const request = new SendMessageRequest('Test Echo');
room.sendMessage(request);
If you are interested in receiving the confirmation or rejection of the request, you can
await
returned promise or usethen()
method:
try {
const message = await room.sendMessage(request);
// Message has been successfully send to chat room
} catch (error) {
// Message request has been rejected, inspect `error` parameter for details.
}
Note: that for this particular request you need to have a
DELETE_MESSAGE
capacity encoded in your chat token.
To trigger a delete-message request:
const request = new DeleteMessageRequest(messageId, 'Reason for deletion');
room.deleteMessage(request);
If you're interested receiving confirmation or rejection of the request, you can
await
returned promise or usethen()
method:
try {
const deleteMessageEvent = await room.deleteMessage(request);
// Message has been successfully deleted from chat room
} catch (error) {
// Delete message request has been rejected, inspect `error` parameter for details.
}
In order to disconnect another user for moderation purposes call disconnectUser()
method.
Note: that for this particular request you need to have a
DISCONNECT_USER
capacity encoded in your chat token.
To disconnect another user for moderation purposes:
const request = new DisconnectUserRequest(userId, 'Reason for disconnecting user');
room.disconnectUser(request);
If you're interested receiving confirmation or rejection of the request, you can await
returned promise or use then()
method:
try {
const disconnectUserEvent = await room.disconnectUser(request);
// User has been successfully disconnected from the chat room
} catch (error) {
// Disconnect user request has been rejected, inspect `error` parameter for details.
}
In order to close your connection to the chat room, call the disconnect()
method on the room
instance:
room.disconnect();
Calling this method will cause room to closes the underlying Web Socket in an orderly manner. The room instance will transition to a disconnected state and will emit a disconnect event, with disconnect reason being set to "clientDisconnect"
.
Amazon Interactive Video Service (IVS) (https://docs.aws.amazon.com/ivs/) Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Provided as AWS Content and subject to the AWS Customer Agreement (https://aws.amazon.com/agreement/) and any other agreement with AWS governing your use of AWS services.
See full license text for more information.
FAQs
The IVS Chat Messaging SDK makes it quick and easy to build excellent chat experience in your JavaScript or TypeScript app.
We found that amazon-ivs-chat-messaging demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).