Web Chat Service - PhenixRTS Docs
Chat Service
The Chat Service provides the ability to:
- Send messages to a room
- Receive messages from a room
- Get a room's chat message history
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.
var chatService = 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. |
| sendMessageToRoom |
(message, callback) |
void |
Send a message to room. |
| getMessages |
(batchSize, afterMessageId, beforeMessageId, callback) |
Array(of [object ChatMessage]) |
Get chat message history |
[object ChatMessage]
| Name |
Type |
Description |
| messageId |
String |
Unique ID of message |
| timestamp |
Number |
Server UTC timestamp of message |
| from |
[object ChatMessageMember] |
Information on the member that sent the message |
| message |
String |
Chat message |
[object ChatMessageMember]
| Name |
Type |
Description |
| sessionId |
String |
Member's session ID |
| screenName |
Number |
Screen name of the member |
| role |
String |
Member's role. See Member Roles |
| lastUpdate |
Number |
Last time member was updated as UTC timestamp |
Listen for Chat Messages in the Active Room
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.
var chatService = new roomService.getChatService();
var chatMessageObservable = chatService.getObservableChatMessages();
var batchSize = 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.
var chatService = new roomService.getChatService();
chatService.start(batchSize);
chatService.sendMessageToRoom(
'My First Message',
function sendMessageCallback(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.
Get the 10 most recent messages after a message:
var batchSize = 10;
var afterMessageId = 'MessageId1';
var beforeMessageId = null;
chatService.getMessages(
batchSize,
afterMessageId,
beforeMessageId,
function getMessagesCallback(error, response) {
if (error) {
// handle error - request messages again
}
if (response.status !== 'ok') {
// handle error - request messages again
}
if (response.status === 'ok' && response.chatMessages) {
// Successfully got chat messages
}
}
);
Get all messages between two messages:
var batchSize = null;
var afterMessageId = 'MessageId1';
var beforeMessageId = 'MessageId2';
if (response.status !== 'ok') {
// handle error - request messages again
}
if (response.status === 'ok' && response.chatMessages) {
// Successfully got chat messages
}
}
);
Get Messages Parameters
| Name |
Type |
Description |
| batchSize (optional) |
Number |
Limit of messages to receive. Used if either afterMessageId or beforeMessageId are provided |
| afterMessageId (optional) |
String |
The messageId to return after. The N messages after message with ID of afterMessageId but before beforeMessageId |
| beforeMessageId (optional) |
String |
The messageId to return before. The N messages before message with ID of beforeMessageId |
| getMessagesCallback (required) |
Function |
Callback with the status of the request |
Get Messages Callback Arguments
| Name |
Type |
Description |
| status |
String |
The status of the operation |
| chatMessages |
[array ChatMessage] |
Array of chat messages matching search criteria |
Get Messages Callback Status Codes
| Status |
Description |
| ok |
Get messages succeeded |
| varies |
Get messages failed for other reasons |