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 # Copied URL First, instantiate the chat service. A Room Service is required to obtain the chat service.
1 import android . content . Context ;
2
3 import com . phenixrts . chat . RoomChatService ;
4 import com . phenixrts . chat . RoomChatServiceFactory ;
5 import com . phenixrts . environment . android . AndroidContext ;
6 import com . phenixrts . room . RoomService ;
7
8
9 final Context context = . . . ;
10 AndroidContext . setContext ( context ) ;
11
12 final RoomService roomService = . . . ;
13
14 final RoomChatService chatService = RoomChatServiceFactory . createRoomChatService ( roomService ) ;
15
16
17
18 final int batchSize = 10 ;
19 final RoomChatService chatServiceWithBatchSize = RoomChatServiceFactory . createRoomChatService (
20 roomService , batchSize ) ;
RoomChatService # Copied URL Name Signature Returns Description 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) void Send a message to room .getMessages (batchSize, afterMessageId, beforeMessageId, callback) void Get chat message history
ChatMessage # Copied URL Name Type Description getMessageId String Unique ID of message getObservableTimeStamp Observable(of Date) Server UTC timestamp of message getObservableFrom Observable(of ChatUser ) Information on the member that sent the message getObservableMessage Observable(of String) Chat message
ChatUser # Copied URL Name Type Description getSessionId String Member's session ID getObservableScreenName Observable(of String) Screen name of the member getObservableMemberRole Observable(of MemberRole) Member's role. See Member Roles getObservableLastUpdate Observable(of Date) Last time member was updated as UTC timestamp
Listen for Chat Messages in the Active Room # Copied URL 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.
1 import com . phenixrts . common . Disposable ;
2 import com . phenixrts . common . Observable ;
3 import com . phenixrts . chat . ChatMessage ;
4 import com . phenixrts . chat . RoomChatService ;
5 import com . phenixrts . chat . RoomChatServiceFactory ;
6 import com . phenixrts . room . RoomService ;
7
8 final RoomService roomService = . . . ;
9
10 final int batchSize = 10 ;
11 final RoomChatService chatService = RoomChatServiceFactory . createRoomChatService ( roomService , batchSize ) ;
12
13 final Observable < ChatMessage [ ] > chatMessageObservable = chatService . getObservableChatMessages ( ) ;
14
15 final Disposable subscription = chatMessageObservable . subscribe ( ( change ) -> {
16 final ChatMessage [ ] messages = ( ChatMessage [ ] ) change ;
17
18 } ) ;
Send a Message to the Active Room # Copied URL 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.
1 import com . phenixrts . common . RequestStatus ;
2 import com . phenixrts . chat . ChatMessage ;
3 import com . phenixrts . chat . RoomChatService ;
4
5 final RoomChatService chatService = . . . ;
6
7
8 chatService . sendMessageToRoom ( "My First Message" ) ;
9
10
11 chatService . sendMessageToRoom ( "My First Message" , ( RequestStatus status , String message ) -> {
12 if ( status == RequestStatus . OK ) {
13
14 } else {
15
16
17 }
18 } ) ;
Send Message Parameters # Copied URL Name Type Description message (required) String Message to send to the room sendMessageCallback (optional) RoomChatService.SendMessageCallback Callback with the status of the request
Send Message Callback Arguments # Copied URL Name Type Description status RequestStatus The status of the operation message String Optional additional error status in case of a failure
Send Message Callback Status Codes # Copied URL Status Description ok Send message succeeded varies Send message failed for other reasons
Chat Message History # Copied URL Get messages older than the most recent N buffered in the chatMessageObservable.
Get the 10 most recent messages after a message:
1 import com . phenixrts . common . RequestStatus ;
2 import com . phenixrts . chat . ChatMessage ;
3 import com . phenixrts . chat . RoomChatService ;
4
5 final RoomChatService chatService = . . . ;
6
7 final int batchSize = 10 ;
8 final String afterMessageId = "MessageId1" ;
9 final String beforeMessageId = null ;
10
11 chatService . getMessages (
12 batchSize ,
13 afterMessageId ,
14 beforeMessageId ,
15 ( RoomChatService chatService , ChatMessage [ ] messages , RequestStatus status ) -> {
16 if ( status == RequestStatus . OK ) {
17
18 } else {
19
20 }
21 } ) ;
Get all messages between two messages:
1 import com . phenixrts . common . RequestStatus ;
2 import com . phenixrts . chat . ChatMessage ;
3 import com . phenixrts . chat . RoomChatService ;
4
5 final RoomChatService chatService = . . . ;
6
7 final int batchSize = 100 ;
8 final String afterMessageId = "MessageId1" ;
9 final String beforeMessageId = "MessageId2" ;
10
11 chatService . getMessages (
12 batchSize ,
13 afterMessageId ,
14 beforeMessageId ,
15 ( RoomChatService chatService , ChatMessage [ ] messages , RequestStatus status ) -> {
16 if ( status == RequestStatus . OK ) {
17
18 } else {
19
20 }
21 } ) ;
Get Messages Parameters # Copied URL Name Type Description batchSize (required) int Limit of messages to receive. 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 callback (required) RoomChatService.GetMessagesCallback Callback with the status of the request
Get Messages Callback Arguments # Copied URL Name Type Description roomChatService RoomChatService The chat service making the callback messages ChatMessage []Array of chat messages matching search criteria status RequestStatus The status of the operation
Get Messages Callback Status Codes # Copied URL Status Description ok Get messages succeeded varies Get messages failed for other reasons