iOS 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. A Room Service is required to obtain the chat service.
import PhenixSdk
let roomService: PhenixRoomService = ... // previously obtained
let chatService = PhenixRoomChatServiceFactory.createRoomChatService(roomService)!
// Use a custom batch size, i.e. max number of message returned by 'getObservableChatMessages'
let batchSize: UInt = 10
let chatServiceWithBatchSize = PhenixRoomChatServiceFactory.createRoomChatService(roomService, batchSize)!
PhenixRoomChatService
| 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
| 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
| 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
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.
import PhenixSdk
let roomService: PhenixRoomService = ... // previously obtained
let batchSize: UInt = 10
let chatService = PhenixRoomChatServiceFactory.createRoomChatService(roomService, batchSize)!
let chatMessageObservable = chatService.getObservableChatMessages();
var subscription = chatMessageObservable?.subscribe({ change in
if let change = change {
let messages = change.value as! [PhenixChatMessage]
// Do something with chat messages
}
})
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.
import PhenixSdk
let chatService: PhenixRoomChatService = ... // previously obtained
// Without callback:
chatService.sendMessage(toRoom: "My First Message")
// With callback
chatService.sendMessage(toRoom: "My First Message") { (status: PhenixRequestStatus, message: String?) in
if status != .ok {
// handle error - send message again
}
}
Send Message Parameters
| 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
| 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
| 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:
import PhenixSdk
let chatService: PhenixRoomChatService = ... // previously obtained
let batchSize: UInt32 = 10
let afterMessageId = "MessageId1"
let beforeMessageId: String? = nil
chatService.getMessages(batchSize, afterMessageId, beforeMessageId) {
(chatService: PhenixRoomChatService?, messages: [PhenixChatMessage]?, status: PhenixRequestStatus) in
if status == .ok, let messages = messages {
// Successfully got chat messages
} else {
// handle error - request messages again
}
}
Get all messages between two messages:
import PhenixSdk
let chatService: PhenixRoomChatService = ... // previously obtained
let batchSize: UInt32 = 100
let afterMessageId = "MessageId1"
let beforeMessageId = "MessageId2"
chatService.getMessages(batchSize, afterMessageId, beforeMessageId) {
(roomChatService: PhenixRoomChatService?, messages: [PhenixChatMessage]?, status: PhenixRequestStatus) in
if status == .ok, let messages = messages {
// Successfully got chat messages
} else {
// handle error - request messages again
}
}
Get Messages Parameters
| 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
| 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
| Status |
Description |
| ok |
Get messages succeeded |
| varies |
Get messages failed for other reasons |