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 PhenixSdk
2
3 let roomService : PhenixRoomService = ...
4
5 let chatService = PhenixRoomChatServiceFactory . createRoomChatService ( roomService ) !
6
7
8
9 let batchSize : UInt = 10
10 let chatServiceWithBatchSize = PhenixRoomChatServiceFactory . createRoomChatService ( roomService , batchSize ) !
PhenixRoomChatService # Copied URL Name Signature Returns Description getObservableChatMessages () Observable(of NSArray of PhenixChatMessage ) 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 bool via NSNumber) 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
PhenixChatMessage # Copied URL Name Type Description getMessageId NSString Unique ID of message getObservableTimeStamp Observable(of NSDate) Server UTC timestamp of message getObservableFrom Observable(of PhenixChatUser ) Information on the member that sent the message getObservableMessage Observable(of NSString) Chat message
PhenixChatUser # Copied URL Name Type Description getSessionId NSString Member's session ID getObservableScreenName Observable(of NSString) Screen name of the member getObservableMemberRole Observable(of PhenixMemberRole) Member's role. See Member Roles getObservableLastUpdate Observable(of NSDate) 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 PhenixSdk
2
3 let roomService : PhenixRoomService = ...
4
5 let batchSize : UInt = 10
6 let chatService = PhenixRoomChatServiceFactory . createRoomChatService ( roomService , batchSize ) !
7
8 let chatMessageObservable = chatService . getObservableChatMessages ( ) ;
9
10 var subscription = chatMessageObservable ? . subscribe ( { change in
11 if let change = change {
12 let messages = change . value as ! [ PhenixChatMessage ]
13
14 }
15 } )
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 PhenixSdk
2
3 let chatService : PhenixRoomChatService = ...
4
5
6 chatService . sendMessage ( toRoom : "My First Message" )
7
8
9 chatService . sendMessage ( toRoom : "My First Message" ) { ( status : PhenixRequestStatus , message : String ? ) in
10 if status != . ok {
11
12 }
13 }
Send Message Parameters # Copied URL Name Type Description message (required) String Message to send to the room sendMessageCallback (optional) Function Callback with the status of the request
Send Message Callback Arguments # Copied URL Name Type Description status PhenixRequestStatus The status of the operation message NSString 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 PhenixSdk
2
3 let chatService : PhenixRoomChatService = ...
4
5 let batchSize : UInt32 = 10
6 let afterMessageId = "MessageId1"
7 let beforeMessageId : String ? = nil
8
9 chatService . getMessages ( batchSize , afterMessageId , beforeMessageId ) {
10 ( chatService : PhenixRoomChatService ? , messages : [ PhenixChatMessage ] ? , status : PhenixRequestStatus ) in
11 if status == . ok , let messages = messages {
12
13 } else {
14
15 }
16 }
Get all messages between two messages:
1 import PhenixSdk
2
3 let chatService : PhenixRoomChatService = ...
4
5 let batchSize : UInt32 = 100
6 let afterMessageId = "MessageId1"
7 let beforeMessageId = "MessageId2"
8
9 chatService . getMessages ( batchSize , afterMessageId , beforeMessageId ) {
10 ( roomChatService : PhenixRoomChatService ? , messages : [ PhenixChatMessage ] ? , status : PhenixRequestStatus ) in
11 if status == . ok , let messages = messages {
12
13 } else {
14
15 }
16 }
Get Messages Parameters # Copied URL Name Type Description batchSize (required) UInt32 Limit of messages to receive. afterMessageId (optional) NSString The messageId to return after. The N messages after message with ID of afterMessageId but before beforeMessageId beforeMessageId (optional) NSString The messageId to return before. The N messages before message with ID of beforeMessageId callback (required) Function Callback with the status of the request
Get Messages Callback Arguments # Copied URL Name Type Description roomChatService PhenixRoomChatService The chat service making the callback messages NSArray of PhenixChatMessage Array of chat messages matching search criteria status PhenixRequestStatus 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