Kotlin
Sariska provides a Kotlin API and easy-to-integrate SDKs for building real-time features into your android applications. Easily add in-app chats, instant messaging, and other real-time functionalities to your apps.
Key Features:
Real-time messaging for in-app chats and instant messaging
Easy installation
Socket creation and management
Channel creation, joining, and leaving
Sending messages, poll votes, and message replies
Presence management (track: typing, joining and leaving users)
History management (fetching chat history and specific messages)
Installation
Step 1 : Install the Phoenix Client
JavaPhoenixClient is hosted on MavenCentral. To install it, you'll need to add the following repositories and dependencies to your project's build configuration:
Repositories
Dependencies
Step 2 : Create Socket
Establish a WebSocket connection to the Sariska server to join channels, receive events, and send messages.
Disconnect Socket
Close the WebSocket connection to the Sariska server. This will terminate all active channels and prevent further communication with the server.
Step 3 : Create Channel
Channels cannot be created directly; instead, they are created through the socket object by calling socket.channel(topic) with the topic of the channel. The topic is a string that identifies the channel, and it can be any string you like.
Channel Prefix
Each channel name starts with a prefix that determines the message serialization format and persistence behavior.
chat: Use this prefix for persisting messages to the database. This prefix requires a fixed schema and is ideal for chat applications.
rtc: Use this prefix for scenarios where message persistence is not necessary. This prefix allows sending arbitrary JSON data, making it suitable for events in multiplayer games, IoT applications, and others.
sariska: Use this prefix for performance-critical applications that utilize Flatbuffers as the serialization format and do not require message persistence. This prefix provides zero-copy serialization/deserialization, significantly enhancing performance.
Step 4 : Join and Leave Channel
To join a channel, call the join() method on the channel object. The join() method returns a promise that resolves when the client has successfully joined the channel. When sending data, you can utilize the .receive() hook to monitor the status of the outgoing push message.
Channel User Joined
Channel New Message
Send Message
Once you've established a connection to a channel, you can start sending messages to other connected clients. To send a message, use the push() method on the channel object.
Send Message Reply
Channel Poll Vote
Attach Media Files to Chat Messages
Attaching media files to chat messages involves obtaining a presigned URL, uploading the file to the presigned URL, and then sending the file information to the chat server.
Obtain a Presigned URL
To obtain a presigned URL, make a POST request to the API endpoint.
Upload the File
After obtaining the presigned URL, the file can be uploaded to the URL using the PUT method.
Chat History
Retrieve the chat history using two methods:
By Subscribing to Events
Subscribe to the
archived_messageevent to receive the last 1000 messages in reverse chronological order.
Subscribe to the
archived_message_countevent to get the total number of messages in the chat history.
To retrieve a list of messages from the chat history, trigger the
archived_messageevent to obtain the messages. Specify thesizeparameter to determine the number of messages you wish to fetch, and set theoffsetparameter as the starting index of the messages.
To receive the total count of messages at any given time, initiate the
archived_message_counttrigger and subscribe to the corresponding event by listening forarchived_message_count
By Using the Messages API
Make a GET request to the API endpoint to fetch the chat history for a specific room.
Fetch Specific Message
Retrieve any specific message from a room. It takes the room ID and message ID as parameters and sends a GET request to the Sariska API to fetch the specified message.
Delete Chat History
Delete chat history for a specific room.
Delete Single or Multiple Messages
Delete All Chats
Presence
The Presence object facilitates real-time synchronization of presence information between the server and the client, enabling the detection of user join and leave events.
Create a Presence Instance
To establish presence synchronization, instantiate a Presence object and provide the channel to track presence lifecycle events:
State Synchronization
Utilize the presence.onSync callback to respond to state changes initiated by the server. For instance, to dynamically render the user list upon every list modification, implement the following:
Handle Individual Join and Leave Events
The presence.onJoin and presence.onLeave callbacks allow for handling individual user join and leave events. Here is an instance:
Retrieve Presence Information
The presence.list(by:) method retrieves a list of presence information based on the local metadata state. By default, it returns the entire presence metadata. Alternatively, a listBy function can be provided to filter the metadata for each presence.
For instance, if a user is online from multiple devices, each with a metadata status of "online," but their status is set to "away" on another device, the application might prefer to display the "away" status in the UI.
The following example defines a listBy function that prioritizes the first metadata registered for each user, representing the first tab or device they used to connect:
User Typing
Send information about a user who is typing. This information can be used to update the chat interface, such as displaying a "user is typing" indicator next to the user's name.
Send User Typing Event
When a user starts typing, the following code sends a typing event to other peers.
Receive User Typing Event
Other peers in the chat can listen for the typing event on the same channel.
For more detailed information on the APIs, check out the Github repository.
For detailed real-time messaging API's, refer to Phoenix documentation.
For detailed management of chat and room APIs, refer to Sariska Swagger documentation
Last updated