• Skip to Search
  • Skip to Content
  • Skip to Side Navigation
Getting StartedSDK ReferenceGlossary
  • Home
  • Getting Started
  • SDK Reference
  • Portal
  • How-To
  • Troubleshooting
  • FAQs
  • Reference
  • Glossary
REST API
Web SDK
Android SDK
iOS SDK
Unity SDK
React Native SDK
EdgeAuth SDK
  • Overview
  • Express API
    • Channel
    • Room
    • PCast™
  • Low-Level API
    • Room
    • Chat
    • PCast™
  • Examples
  • Release Notes

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. A Room Service is required to obtain the chat service.

Java
1import android.content.Context;
2
3import com.phenixrts.chat.RoomChatService;
4import com.phenixrts.chat.RoomChatServiceFactory;
5import com.phenixrts.environment.android.AndroidContext;
6import com.phenixrts.room.RoomService;
7
8// IMPORTANT: Before accessing any of the static factories, make sure the context is passed to Phenix:
9final Context context = ...; // e.g. Activity.getApplication();
10AndroidContext.setContext(context);
11
12final RoomService roomService = ...; // previously obtained
13
14final RoomChatService chatService = RoomChatServiceFactory.createRoomChatService(roomService);
15
16
17// Use a custom batch size, i.e. max number of message returned by 'getObservableChatMessages'
18final int batchSize = 10;
19final RoomChatService chatServiceWithBatchSize = RoomChatServiceFactory.createRoomChatService(
20 roomService, batchSize);

RoomChatService

NameSignatureReturnsDescription
getObservableChatMessages()Observable(of ChatMessage[])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)voidSend a message to room.
getMessages(batchSize, afterMessageId, beforeMessageId, callback)voidGet chat message history

ChatMessage

NameTypeDescription
getMessageIdStringUnique ID of message
getObservableTimeStampObservable(of Date)Server UTC timestamp of message
getObservableFromObservable(of ChatUser)Information on the member that sent the message
getObservableMessageObservable(of String)Chat message

ChatUser

NameTypeDescription
getSessionIdStringMember's session ID
getObservableScreenNameObservable(of String)Screen name of the member
getObservableMemberRoleObservable(of MemberRole)Member's role. See Member Roles
getObservableLastUpdateObservable(of Date)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.

Java
1import com.phenixrts.common.Disposable;
2import com.phenixrts.common.Observable;
3import com.phenixrts.chat.ChatMessage;
4import com.phenixrts.chat.RoomChatService;
5import com.phenixrts.chat.RoomChatServiceFactory;
6import com.phenixrts.room.RoomService;
7
8final RoomService roomService = ...; // previously obtained
9
10final int batchSize = 10;
11final RoomChatService chatService = RoomChatServiceFactory.createRoomChatService(roomService, batchSize);
12
13final Observable<ChatMessage[]> chatMessageObservable = chatService.getObservableChatMessages();
14
15final Disposable subscription = chatMessageObservable.subscribe((change) -> {
16 final ChatMessage[] messages = (ChatMessage[])change;
17 // Do something with chat messages
18});

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.

Java
1import com.phenixrts.common.RequestStatus;
2import com.phenixrts.chat.ChatMessage;
3import com.phenixrts.chat.RoomChatService;
4
5final RoomChatService chatService = ...; // previously obtained
6
7// Without callback:
8chatService.sendMessageToRoom("My First Message");
9
10// With callback
11chatService.sendMessageToRoom("My First Message", (RequestStatus status, String message) -> {
12 if (status == RequestStatus.OK) {
13 // Successfully sent
14 } else {
15 // handle error - send message again
16 // (`message` may provide more information about what went wrong)
17 }
18});

Send Message Parameters

NameTypeDescription
message (required)StringMessage to send to the room
sendMessageCallback (optional)RoomChatService.SendMessageCallbackCallback with the status of the request

Send Message Callback Arguments

NameTypeDescription
statusRequestStatusThe status of the operation
messageStringOptional additional error status in case of a failure

Send Message Callback Status Codes

StatusDescription
okSend message succeeded
variesSend 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:

Java
1import com.phenixrts.common.RequestStatus;
2import com.phenixrts.chat.ChatMessage;
3import com.phenixrts.chat.RoomChatService;
4
5final RoomChatService chatService = ...; // previously obtained
6
7final int batchSize = 10;
8final String afterMessageId = "MessageId1";
9final String beforeMessageId = null;
10
11chatService.getMessages(
12 batchSize,
13 afterMessageId,
14 beforeMessageId,
15 (RoomChatService chatService, ChatMessage[] messages, RequestStatus status) -> {
16 if (status == RequestStatus.OK) {
17 // Successfully got chat messages
18 } else {
19 // handle error - request messages again
20 }
21});

Get all messages between two messages:

Java
1import com.phenixrts.common.RequestStatus;
2import com.phenixrts.chat.ChatMessage;
3import com.phenixrts.chat.RoomChatService;
4
5final RoomChatService chatService = ...; // previously obtained
6
7final int batchSize = 100;
8final String afterMessageId = "MessageId1";
9final String beforeMessageId = "MessageId2";
10
11chatService.getMessages(
12 batchSize,
13 afterMessageId,
14 beforeMessageId,
15 (RoomChatService chatService, ChatMessage[] messages, RequestStatus status) -> {
16 if (status == RequestStatus.OK) {
17 // Successfully got chat messages
18 } else {
19 // handle error - request messages again
20 }
21});

Get Messages Parameters

NameTypeDescription
batchSize (required)intLimit of messages to receive.
afterMessageId (optional)StringThe messageId to return after. The N messages after message with ID of afterMessageId but before beforeMessageId
beforeMessageId (optional)StringThe messageId to return before. The N messages before message with ID of beforeMessageId
callback (required)RoomChatService.GetMessagesCallbackCallback with the status of the request

Get Messages Callback Arguments

NameTypeDescription
roomChatServiceRoomChatServiceThe chat service making the callback
messagesChatMessage[]Array of chat messages matching search criteria
statusRequestStatusThe status of the operation

Get Messages Callback Status Codes

StatusDescription
okGet messages succeeded
variesGet messages failed for other reasons
Page Content
    Copyright 2023 © Phenix RTS
    Privacy Policy | Terms of Service
    v2023-01-31T21:25:10