All room members that are subscribed to the chat will receive the text messages. You must first enter the room before you may send and receive messages.
Initializing
First, instantiate the chat service. The Room Service exposes a method to get the chat associated with the active room.
JavaScript
varchatService=roomService.getChatService();
[object ChatService]
Name
Signature
Returns
Description
start
(batchSize)
void
Start receiving messages and store the most recent of size batchSize
stop
()
void
Stop receiving messages
getObservableChatMessages
()
Observable(of Array)
Get the observable of the most recent chat messages. This is a buffer of the most recent N messages up to a max of 100. See Listen for Chat Messages
getObservableChatEnabled
()
Observable(of Boolean)
Get the observable of the enabled status of the chat service. Use this to disable or enable the chat.
The chat service exposes an observable of the most recent N (batchSize) messages in the active room up to a max of 100 messages. To view older messages you will need to use the getMessages method.
JavaScript
varchatService=newroomService.getChatService();varchatMessageObservable=chatService.getObservableChatMessages();varbatchSize=10;chatMessageObservable.subscribe(function (mostRecentChatMessages) {// Do something with chat messages// mostRecentChatMessages is an array of messages// The messages in the array are not guaranteed to be in order, and may contain duplicates});chatService.start(batchSize);
Send a Message to the Active Room
Send a single message to the active room. If the message is successfully sent the chatMessageObservable will trigger notifications on all subscribers with the new chat message.
JavaScript
varchatService=newroomService.getChatService();chatService.start(batchSize);chatService.sendMessageToRoom('My First Message',functionsendMessageCallback(error, response) {if (error) {// handle error - send message again }if (response.status!=='ok') {// handle error - send message again }if (response.status==='ok') {// Successfully sent message } });
Send Message Parameters
Name
Type
Description
message (required)
String
Message to send to the room
sendMessageCallback (required)
Function
Callback with the status of the request
Send Message Callback Arguments
Name
Type
Description
status
String
The status of the operation
Send Message Callback Status Codes
Status
Description
ok
Send message succeeded
varies
Send message failed for other reasons
Chat Message History
Get messages older than the most recent N buffered in the chatMessageObservable.