C# (Unity Engine)


Sariska provides a C# API and easy-to-integrate SDKs for Unity and Blazor to add real-time features in your applications.

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 NuGetForUnity Package Manager

  • Download the package manager from GitHub

  • In your Unity project, navigate to Assets -> Import Package -> Custom Package

  • Select the downloaded "NugetForUnity.3.0.4.unitypackage" and click Import All

Step 2 : Install Phoenix Client Package

  • Access NuGet Package Manager: Click NuGet (at the top of your Unity Editor) -> Manage NuGet Packages

  • Search for "io.sariska.sariskacsharp"

  • Click Install to add the Phoenix Client package to your project.

Step 3 : Import PhoenixSharp

Step 4 : Create Socket

Establish a WebSocket connection to the Sariska server to join channels, receive events, and send messages.

To disconnect the user from any previous WebSocket connections and tabs before opening a new one, set the disconnect_past_tabs parameter to true in the WebSocket connection.

Disconnect Socket

Close the WebSocket connection to the Sariska server. This will terminate all active channels and prevent further communication with the server.

Step 5 : 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.

Handle Errors

When an error occurs on the channel, the ERROR callback is triggered. The callback receives the error information in payload, if available.

Close Channel

When the channel is closed, the CLOSE callback is invoked. This signifies that the communication on the channel has ended and no further data can be exchanged.

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

Payload of user containing user details, including:
  • id: The user's ID

  • name: The user's name

  • avatar: The user's avatar URL

  • email: The user's email address

Payload of room containing room details, including:
  • id: The room's ID

  • session_id: The room's session ID

  • created_by: The ID of the user who created the room

  • status: The status of the room (e.g., "active", "inactive")

Channel New Message

Payload of message containing message details, including:
  • content: The message content

  • id: The sender's user ID

  • name: The sender's name

  • content_type: The message content type (media, emoji, text)

  • is_delivered: Whether the message has been delivered to the recipient

  • created_by: The ID of the user who sent the message

  • created_by_name: The name of the user who sent the message

  • timestamp: The message timestamp

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

Payload of vote containing vote details, including:
  • content: The vote question

  • content_type: The vote content type (media, emoji, text)

  • options: An array of vote options

For other polls APIs, please refer to Swagger documentation

Unity is a cross-platform game engine developed by Unity Technologies.

The (C#)Phoenix Channel client can be used to build real time messaging apps for:

  • Unity

  • Blazor

Installation

The first step is to install NuGetForUnity Package manager in your Unity project. The Package Manager is used to import Phoenix Client into your project.

  1. The package manager can be downloaded by clicking on the following link. https://github.com/GlitchEnzo/NuGetForUnity/releases/download/v3.0.4/NugetForUnity.3.0.4.unitypackage

  2. In your unity project, go to Assets -> Import-Package -> Custom Package

  3. Select "NugetForUnity.3.0.4.unitypackage" from your download folder and import all.

  4. Once imported, click on NuGet (top of your Unity Editor) -> Manage Nuget Packages.

  5. Search for "io.sariska.sariskacsharp" and install.

Socket

Importing PhoenixSharp into your script

Creating a Socket

First, you need to create the socket that will join channels, receive events, and send messages. A single connection is established to the server and channels are multiplexed over the connection.

Disconnect Socket

Channels

Creating a Channel

Once your socket is created, you can join channel topics that listen for specific events and allow for sending data to do a topic. Whenever sending data, you can use the .receive() hook to get the status of the outbound push message. Channels cannot be created directly but instead are created through the socket object by calling socket.channel(topic) with the topic of the channel.

Sending Messages

Sending Poll Votes

For other polls apis checkout swagger

Sending Message Reply

User Typing

Sending user typing event to other peers

other peers now can listen for message typing event

Presence

The Presence object provides features for syncing presence information from the server with the client and handling presences of joining and leaving.

Creating Presence

To sync presence state from the server, first instantiate an object and pass your channel in to track lifecycle events:

Syncing State

Next, use the presence.onSync callback to react to state changes from the server. For example, to render the list of users every time the list changes, you could write:

Listing Presences

presence.list(by:) is used to return a list of presence information based on the local state of metadata. By default, all presence metadata is returned, but a listBy function can be supplied to allow the client to select which metadata to use for a given presence. For example, you may have a user online from different devices with a metadata status of "online", but they have set themselves to "away" on another device. In this case, the app may choose to use the "away" status for what appears on the UI. The example below defines a listBy function that prioritizes the first metadata which was registered for each user. This could be the first tab they opened or the first device they came online from:

Handling individual join and leave events

The presence.onJoin and presence.onLeave callbacks can be used to react to individual presences joining and leaving the app.

History

Retrieve the chat history using two methods:

  1. By Subscribing to Events

  • Subscribe to the archived_message event to receive the last 1000 messages in reverse chronological order.

  • Subscribe to the archived_message_count event to get the total number of messages in the chat history.

  • To retrieve a list of messages in the chat history, trigger the "archived_message" event to obtain the messages. Specify the "size" parameter to determine the number of messages you wish to fetch, and set the "offset" parameter as the starting index of the messages and group_by_day to group messages by date.

  • If you desire to receive the total count of messages at any given time, initiate the "archived_message_count" trigger and subscribe to the corresponding event by listening for "archived_message_count."

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

Attachments

You can attach media files to chat as follows:

For more detailed information on the APIs, check out the Phoenix Demo for Unity.

For detailed real-time messaging API's , follow Phoenix documentation. For detailed management of chat and room APIs, refer to Sariska Swagger Documentation

Last updated