• 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.

Swift
1import PhenixSdk
2
3let roomService: PhenixRoomService = ... // previously obtained
4
5let chatService = PhenixRoomChatServiceFactory.createRoomChatService(roomService)!
6
7
8// Use a custom batch size, i.e. max number of message returned by 'getObservableChatMessages'
9let batchSize: UInt = 10
10let chatServiceWithBatchSize = PhenixRoomChatServiceFactory.createRoomChatService(roomService, batchSize)!

PhenixRoomChatService

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

PhenixChatMessage

NameTypeDescription
getMessageIdNSStringUnique ID of message
getObservableTimeStampObservable(of NSDate)Server UTC timestamp of message
getObservableFromObservable(of PhenixChatUser)Information on the member that sent the message
getObservableMessageObservable(of NSString)Chat message

PhenixChatUser

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

Swift
1import PhenixSdk
2
3let roomService: PhenixRoomService = ... // previously obtained
4
5let batchSize: UInt = 10
6let chatService = PhenixRoomChatServiceFactory.createRoomChatService(roomService, batchSize)!
7
8let chatMessageObservable = chatService.getObservableChatMessages();
9
10var subscription = chatMessageObservable?.subscribe({ change in
11 if let change = change {
12 let messages = change.value as! [PhenixChatMessage]
13 // Do something with chat messages
14 }
15 })

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.

Swift
1import PhenixSdk
2
3let chatService: PhenixRoomChatService = ... // previously obtained
4
5// Without callback:
6chatService.sendMessage(toRoom: "My First Message")
7
8// With callback
9chatService.sendMessage(toRoom: "My First Message") { (status: PhenixRequestStatus, message: String?) in
10 if status != .ok {
11 // handle error - send message again
12 }
13}

Send Message Parameters

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

Send Message Callback Arguments

NameTypeDescription
statusPhenixRequestStatusThe status of the operation
messageNSStringOptional 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:

Swift
1import PhenixSdk
2
3let chatService: PhenixRoomChatService = ... // previously obtained
4
5let batchSize: UInt32 = 10
6let afterMessageId = "MessageId1"
7let beforeMessageId: String? = nil
8
9chatService.getMessages(batchSize, afterMessageId, beforeMessageId) {
10 (chatService: PhenixRoomChatService?, messages: [PhenixChatMessage]?, status: PhenixRequestStatus) in
11 if status == .ok, let messages = messages {
12 // Successfully got chat messages
13 } else {
14 // handle error - request messages again
15 }
16}

Get all messages between two messages:

Swift
1import PhenixSdk
2
3let chatService: PhenixRoomChatService = ... // previously obtained
4
5let batchSize: UInt32 = 100
6let afterMessageId = "MessageId1"
7let beforeMessageId = "MessageId2"
8
9chatService.getMessages(batchSize, afterMessageId, beforeMessageId) {
10 (roomChatService: PhenixRoomChatService?, messages: [PhenixChatMessage]?, status: PhenixRequestStatus) in
11 if status == .ok, let messages = messages {
12 // Successfully got chat messages
13 } else {
14 // handle error - request messages again
15 }
16}

Get Messages Parameters

NameTypeDescription
batchSize (required)UInt32Limit of messages to receive.
afterMessageId (optional)NSStringThe messageId to return after. The N messages after message with ID of afterMessageId but before beforeMessageId
beforeMessageId (optional)NSStringThe messageId to return before. The N messages before message with ID of beforeMessageId
callback (required)FunctionCallback with the status of the request

Get Messages Callback Arguments

NameTypeDescription
roomChatServicePhenixRoomChatServiceThe chat service making the callback
messagesNSArray of PhenixChatMessageArray of chat messages matching search criteria
statusPhenixRequestStatusThe 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