Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Chat Client

Jack Cook edited this page Mar 20, 2016 · 1 revision

The chat client interfaces with Beam's chat servers, allowing your app to send and receive chat messages.

Creating a Connection

To connect to the chat server, first initialize ChatClient, passing a delegate of ChatClientDelegate. If there is a user authenticated with BeamSession, then they will automatically be authenticated with the chat server. Otherwise, a generic connection to the server will be made, in which the user can't participate in chat.

let chatClient = ChatClient(delegate: self)
chatClient.joinChannel(channel.id)

Delegate Methods

The delegate methods will be passing your app data about the connection you have with the chat server in the meantime, which you should listen to and use to update your app accordingly.

chatDidConnect

The chatDidConnect: delegate method will be fired when a connection is made to the chat server. At this point in time, packets and messages are able to be sent through the client.

func chatDidConnect() {
    print("connected to chat successfully")
}

chatReceivedPacket

The chatReceivedPacket:packet: delegate method will be fired when a packet has been received from the chat server. This packet has already been parsed and interpreted by the API client, allowing you to use packet data effortlessly.

func chatReceivedPacket(packet: Packet) {
    if let packet = packet as? MessagePacket {
        print("received a chat message")
    }
}

updateWithViewers

The updateWithViewers:viewers: delegate method is fired when a new viewer count has been received, regardless of whether it has changed from the last time this method was fired.

func updateWithViewers(viewers: Int) {
    print("\(viewers) are watching the stream")
}
Clone this wiki locally