PhoenixSharp 1.2.3
dotnet add package PhoenixSharp --version 1.2.3
NuGet\Install-Package PhoenixSharp -Version 1.2.3
<PackageReference Include="PhoenixSharp" Version="1.2.3" />
<PackageVersion Include="PhoenixSharp" Version="1.2.3" />
<PackageReference Include="PhoenixSharp" />
paket add PhoenixSharp --version 1.2.3
#r "nuget: PhoenixSharp, 1.2.3"
#:package PhoenixSharp@1.2.3
#addin nuget:?package=PhoenixSharp&version=1.2.3
#tool nuget:?package=PhoenixSharp&version=1.2.3
<p align="center"> <img width="340" height="271" alt="phoenix-sharp-splash" src="https://github.com/user-attachments/assets/3977a485-02cb-49a4-b484-b3320d21355a" /> </p>
<p align="center"> <a href="https://github.com/Mazyod/PhoenixSharp/actions/workflows/dotnet.yml"><img src="https://github.com/Mazyod/PhoenixSharp/actions/workflows/dotnet.yml/badge.svg" alt=".NET" /></a> <a href="https://codecov.io/gh/Mazyod/PhoenixSharp"><img src="https://codecov.io/gh/Mazyod/PhoenixSharp/branch/master/graph/badge.svg" alt="codecov" /></a> <a href="https://www.nuget.org/packages/PhoenixSharp"><img src="https://img.shields.io/nuget/v/PhoenixSharp" alt="NuGet" /></a> <a href="https://openupm.com/packages/io.level3.phoenixsharp/"><img src="https://img.shields.io/npm/v/io.level3.phoenixsharp?label=openupm®istry_uri=https://package.openupm.com" alt="OpenUPM" /></a> <img src="https://img.shields.io/badge/netstandard-2.0-blue" alt="netstandard 2.0" /> </p>
<p align="center"> <strong>A C# client for Phoenix Channels.</strong><br> Unity compatible. Powering <a href="http://level3.io">Dama King</a>. </p>
Features
- Full Phoenix Channels protocol support
- Modern async/await API with cancellation support
- Automatic reconnection and channel rejoin
- Presence tracking
- Customizable WebSocket and JSON implementations
- Unity and .NET Standard 2.0 compatible
Installation
NuGet
dotnet add package PhoenixSharp
Unity (OpenUPM)
Install via openupm-cli:
openupm add io.level3.phoenixsharp
Or add manually to Packages/manifest.json:
{
"scopedRegistries": [
{
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": ["io.level3"]
}
],
"dependencies": {
"io.level3.phoenixsharp": "1.2.3"
}
}
Alternatively, download the source and add it to your project, or use a Git submodule.
Quick Start
// Create and connect a socket
var socket = new Socket(
"wss://example.com/socket",
new Socket.Options(new JsonMessageSerializer())
);
socket.Connect();
// Join a channel
var channel = socket.Channel("room:lobby", new { userId = "123" });
channel.Join();
// Listen for events
channel.On("new_message", message => {
var payload = message.Payload.Unbox<ChatMessage>();
Console.WriteLine($"Received: {payload.Text}");
});
// Send messages
channel.Push("send_message", new { text = "Hello!" });
Async API
await socket.ConnectAsync();
var result = await channel.JoinAsync();
if (result.IsSuccess)
{
var response = await channel.PushAsync<ChatMessage>(
"send_message",
new { text = "Hello!" }
);
if (response.IsSuccess)
Console.WriteLine($"Sent with id: {response.Response.Id}");
}
await channel.LeaveAsync();
Documentation
- Migration Guide - Upgrading from older versions
- Integration Tests - Complete usage examples
WebSocket Implementation
The library requires an IWebsocket implementation. Sample implementations are available in PhoenixTests/WebSocketImpl.
var factory = new MyWebSocketFactory();
var socket = new Socket(address, null, factory, options);
Recommended implementations:
- BestHTTP (Unity) - See
Reference/Unity/BestHTTP - WebSocketSharp - Good cross-platform option
- System.Net.WebSockets - Built-in .NET option
JSON Serialization
The default JsonMessageSerializer uses Newtonsoft.Json with the Phoenix V2 serialization format. To use a different serializer, implement IMessageSerializer and IJsonBox.
var options = new Socket.Options(new MyCustomSerializer());
Presence
var presence = new Presence(channel);
presence.OnJoin += (key, current, newPresence) => {
Console.WriteLine($"{key} joined");
};
presence.OnLeave += (key, current, leftPresence) => {
Console.WriteLine($"{key} left");
};
// Wait for initial state (async)
await presence.WaitForInitialSyncAsync();
Unity Notes
For background on Unity's .NET support, see Microsoft's Unity scripting documentation.
Threading
By default, callbacks use System.Threading.Tasks which works with Unity's SynchronizationContext. For more control, implement IDelayedExecutor (or use UniTask for better performance):
var options = new Socket.Options(serializer)
{
DelayedExecutor = new CoroutineDelayedExecutor()
};
See Reference/Unity for a coroutine-based implementation.
Recommended Libraries
- BestHTTP - Handles threading automatically
- Json.NET for Unity - More reliable than raw Newtonsoft.Json on mobile
API Reference
Socket
| Method | Description |
|---|---|
Connect() |
Connect to the server |
ConnectAsync() |
Connect asynchronously |
Disconnect() |
Disconnect from the server |
DisconnectAsync() |
Disconnect asynchronously |
Channel(topic, params) |
Create a channel |
Channel
| Method | Description |
|---|---|
Join() |
Join the channel |
JoinAsync() |
Join asynchronously, returns JoinResult |
Leave() |
Leave the channel |
LeaveAsync() |
Leave asynchronously |
Push(event, payload) |
Send a message |
PushAsync(event, payload) |
Send asynchronously, returns PushResult |
PushAsync<T>(event, payload) |
Send with typed response |
On(event, callback) |
Subscribe to events |
Off(event) |
Unsubscribe from events |
WaitForEventAsync(event) |
Wait for a single event |
Presence
| Method | Description |
|---|---|
OnJoin |
Event fired when a user joins |
OnLeave |
Event fired when a user leaves |
OnSync |
Event fired on state sync |
WaitForInitialSyncAsync() |
Wait for initial presence state |
WaitForUserAsync(key, timeout) |
Wait for a specific user |
Running Tests
# All tests
dotnet test
# Unit tests only
dotnet test --filter "Category!=Integration"
# Integration tests (requires server)
dotnet test --filter "Category=Integration"
Integration tests run against phoenix-sharp.level3.io:3080. Server source: phoenix-integration-tester
Contributing
Issues and pull requests are welcome!
License
MIT
Author
Maz (Mazyad Alabduljaleel)
<p align="center"> <sub>Logo is a mix of Unity and Phoenix logos. Please don't sue me.</sub> </p>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.