Chat events (Socket.IO)
Interacting with the Karaz Platform realtime chat system is done using the Socket.IO framework (v4), the docs for which can be found here. Skim through the documentation to learn how to:
- Establish a connection with a Socket.IO server
- Pass metadata to the Socket.IO server
- Register listeners for events
- Emit payloads to the Socket.IO server
...among other things using their language specific SDKs.
Here is an Android specific guide on how to use Socket.IO, and here is an iOS specific guide.
Events
Events are tagged in this guide by one of two categories: Client Event or Server Event.
We define Client Events as events that are meant for the client to listen to. The client needs to register
a listener for each event. When the server emits a payload to the client, these listeners will
be triggered. The schema of the received payload will depend on the event, and are outlined below.
As previously mentioned, every Client Event in this guide is tagged with the Client Event tag.
We define Server Events as events that are meant for the server to listen to, and for clients to send
payloads on. When the client emits a payload to the server, they need to specify the event name.
The schema of this payload will depend on the event. As previously mentioned, every Server Event
in this guide is tagged with the Server Event tag.
Rooms & Sessions
Since we're characterising the term "room" as the information about the room that is stored on the database, we need a word to describe the ephemeral sessions where the members of a room can communicate in realtime. We're simply calling these websocket sessions, or "sessions" for short.
Every session is uniquely identified by a key. This key is generated using the room's ID and a secret stored on the server. Multiple users can be connected to the same session, and a single user can connect to multiple sessions.
We refer to other users in a session as peers.
Even though we are calling them "sessions", in our payload attributes we are still calling the keys for these sessions "roomKeys", and we prefix our session related events with "room:".
Intended usage
Users must hit our REST API for session keys. They will only receive keys for rooms they are a member of, with the exception of when they are creating a room. In this case, they can simply request our REST API to create a room, which will return a key that the client can use to create a session. They must then put these keys in their message payloads to tell the server which session a message is for.
Headers
The client must send the following required headers when first connecting to the Socket.IO server:
- Name
userid- Type
- string
- Description
The ID of the user connecting to the Socket.IO server.
If any of these headers are missing, the Socket.IO server will forcefully disconnect the client.
Join sessions
Send a payload on the "room:join*" event to join multiple sessions.
Payload attributes
- Name
roomKeys- Type
- string[]
- Description
A list of keys for multiple sessions.
Payload
{
"roomKeys": [
"NdiOkCerFQCJyIXTqQo4cArULFzRnQuIvq1+8EjIf0=",
"YnbTbghNRFQCJyIXTqQo4cArULFzRnQuIvq1+RhrllQ="
]
}
Add peers
Send a payload on the "room:add_peers*" event to add peers to a session.
Payload attributes
- Name
roomKey- Type
- string
- Description
An identifier for the session.
- Name
userIDs- Type
- string[]
- Description
A list of IDs of the peers to add to the session. Note the following:
- If the list contains entries of the client's user ID, those entries will be filtered out before processing.
- If the list is empty (whether due to being filtered or otherwise), the session will not be created.
Payload
{
"roomKey": "YnbTbghNRFQCJyIXTqQo4cArULFzRnQuIvq1+RhrllQ=",
"userIDs": ["D4c7p43FkVmElcbAAAAF", "tTlSu7juwfKFiMkEAAAB"]
}
Leave session
Send a payload on the "room:leave" event to leave a session.
Payload attributes
- Name
roomKey- Type
- string
- Description
The key to identify which session to leave.
Payload
{
"roomKey": "YnbTbghNRFQCJyIXTqQo4cArULFzRnQuIvq1+RhrllQ=",
}
Send message
Send a payload on the "msg:send" event to send a message to a session.
Payload attributes
- Name
roomKey- Type
- string
- Description
The key to identify which session to send the message to.
- Name
senderName- Type
- string
- Description
Name of the sender.
- Name
type- Type
- string
- Description
The type of message being sent. One of
"text","file", or"image":- If the type is
"text", then the"file"and"image"fields will be set tonullbefore the message is sent to peers in the session. - If the type is
"file"or"image", either the"file"or"image"field will be set tonullbefore the message is sent to peers in the session. - If the type is none of the above, the message is dropped.
- If the type is
- Name
text- Type
- string
- Description
The text contents of the message.
- Name
file- Type
- string | null
- Description
A URL for a file being sent.
- Name
image- Type
- string | null
- Description
A URL for an image being sent.
- Name
createdAt- Type
- integer
- Description
The Unix timestamp of when the message was created.
Payload
{
"roomKey": "YnbTbghNRFQCJyIXTqQo4cArULFzRnQuIvq1+RhrllQ=",
"senderName": "John Doe",
"type": "file",
"text": "Hello World!",
"file": "https://example.com/file.txt",
"image": "",
"createdAt": 1234567890
}
Added to session
Listeners on the "room:added" event are fired when the client is added to a session.
Payload attributes
- Name
roomKey- Type
- string
- Description
The key for the session that the client was added to.
Payload
{
"roomKey": "YnbTbghNRFQCJyIXTqQo4cArULFzRnQuIvq1+RhrllQ=",
}
Peers joined session
Listeners on the "room:new_peers*" event are fired when peers join a session
that the client is currently in.
Payload attributes
- Name
online- Type
- string[]
- Description
IDs of users that joined the session.
- Name
offline- Type
- string[]
- Description
IDs of users that joined the room but are currently offline.
Payload
{
"online": ["D4c7p43FkVmElcbAAAAF", "tTlSu7juwfKFiMkEAAAB"],
"offline": ["pqs3MKKRDvfV7gOAAAAB"]
}
Peer left room
Listeners on the "room:peer_removed" event are fired when a peer is removed
from a room that the user is in. This can be due to the user removing themselves
or if they are kicked by another user. Since they are removed from the room,
they are also removed from the session.
Payload attributes
- Name
roomKey- Type
- string
- Description
The key generated from the room that the peer was removed from.
- Name
userID- Type
- string
- Description
The ID of the user that was removed from the room.
Payload
{
"roomKey": "YnbTbghNRFQCJyIXTqQo4cArULFzRnQuIvq1+RhrllQ=",
"userID": "D4c7p43FkVmElcbAAAAF"
}
Message sent
Listeners on the "msg:sent" event are fired when a message is sent to a session
that the client is currently in.
Payload attributes
- Name
roomKey- Type
- string
- Description
The key to identify which session to the message was sent to.
- Name
senderID- Type
- string
- Description
User ID of the sender.
- Name
senderName- Type
- string
- Description
Name of the sender.
- Name
type- Type
- string
- Description
The type of message being sent. One of
"text","file", or"image":- This field is guaranteed to always be either
"text","file", or"image". - If the type is
"text", then the"file"and"image"fields will be set tonull. - If the type is
"file"or"image", either the"file"or"image"field will be set tonull. - If the type is none of the above, the message is dropped.
- This field is guaranteed to always be either
- Name
text- Type
- string
- Description
The text contents of the message.
- Name
file- Type
- string | null
- Description
A URL for the file sent.
May be
nullif the message type is"text"or"image".
- Name
image- Type
- string | null
- Description
A URL for the image sent.
May be
nullif the message type is"text"or"file".
- Name
createdAt- Type
- integer
- Description
The Unix timestamp of when the message was created.
Payload
{
"roomKey": "YnbTbghNRFQCJyIXTqQo4cArULFzRnQuIvq1+RhrllQ=",
"senderID": "D4c7p43FkVmElcbAAAAF",
"senderName": "John Doe",
"type": "file",
"text": "Hello World!",
"file": "https://example.com/file.txt",
"image": null,
"createdAt": 1234567890
}
Peer went offline
Listeners on the "room:peer_disconnected" event are fired when some peer that
was in a session with the client has gone offline.
Payload attributes
- Name
userID- Type
- string
- Description
The ID of the user that went offline.
Payload
{
"userID": "D4c7p43FkVmElcbAAAAF"
}
