This is a .NET library for interacting with the Channels WebSocket API.
Register at pusher.com/channels and use the application credentials within your app as shown below.
More general documentation can be found on the Official Channels Documentation.
For integrating Pusher Channels with Unity follow the instructions at https://github.com/pusher/pusher-websocket-unity
- .NET Standard 1.6
- Unity 2018 and greater via pusher-websocket-unity
Install-Package PusherClient
See the example app for full details.
_pusher = new Pusher("YOUR_APP_KEY");
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
_pusher.Connect();
where _pusher_ConnectionStateChanged
and _pusher_Error
are custom event handlers such as
static void _pusher_ConnectionStateChanged(object sender, ConnectionState state)
{
Console.WriteLine("Connection state: " + state.ToString());
}
static void _pusher_Error(object sender, PusherException error)
{
Console.WriteLine("Pusher Channels Error: " + error.ToString());
}
_pusher = new Pusher("YOUR_APP_KEY");
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
ConnectionState connectionState = await _pusher.ConnectAsync();
In the case of the async version, state and error changes will continue to be published via events, but the initial connection state will be returned from the ConnectAsync()
method.
If you have an authentication endpoint for private or presence channels:
_pusher = new Pusher("YOUR_APP_KEY", new PusherOptions(){
Authorizer = new HttpAuthorizer("YOUR_ENDPOINT")
});
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
_pusher.Connect();
_pusher = new Pusher("YOUR_APP_KEY", new PusherOptions(){
Authorizer = new HttpAuthorizer("YOUR_ENDPOINT")
});
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
ConnectionState connectionState = await _pusher.ConnectAsync();
If you are on a non default cluster (e.g. eu):
_pusher = new Pusher("YOUR_APP_KEY", new PusherOptions(){
Cluster = "eu"
});
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
_pusher.Connect();
_pusher = new Pusher("YOUR_APP_KEY", new PusherOptions(){
Cluster = "eu"
});
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
ConnectionState connectionState = _pusher.ConnectAsync();
_myChannel = _pusher.Subscribe("my-channel");
_myChannel.Subscribed += _myChannel_Subscribed;
where _myChannel_Subscribed
is a custom event handler such as
static void _myChannel_Subscribed(object sender)
{
Console.WriteLine("Subscribed!");
}
Channel _myChannel = await _pusher.SubscribeAsync("my-channel");
_myChannel.Bind("my-event", (dynamic data) =>
{
Console.WriteLine(data.message);
});
_presenceChannel = (PresenceChannel)_pusher.Subscribe("presence-channel");
_presenceChannel.Subscribed += _presenceChannel_Subscribed;
_presenceChannel.MemberAdded += _presenceChannel_MemberAdded;
_presenceChannel.MemberRemoved += _presenceChannel_MemberRemoved;
Where _presenceChannel_Subscribed
, _presenceChannel_MemberAdded
, and _presenceChannel_MemberRemoved
are custom event handlers such as
static void _presenceChannel_MemberAdded(object sender, KeyValuePair<string, dynamic> member)
{
Console.WriteLine((string)member.Value.name.Value + " has joined");
ListMembers();
}
static void _presenceChannel_MemberRemoved(object sender)
{
ListMembers();
}
_presenceChannel = await (PresenceChannel)_pusher.SubscribeAsync("presence-channel");
_presenceChannel.Subscribed += _presenceChannel_Subscribed;
_presenceChannel.MemberAdded += _presenceChannel_MemberAdded;
_presenceChannel.MemberRemoved += _presenceChannel_MemberRemoved;
Remove a specific callback:
_myChannel.Unbind("my-event", callback);
Remove all callbacks for a specific event:
_myChannel.Unbind("my-event");
Remove all bindings on the channel:
_myChannel.UnbindAll();
You should be familiar with creating an publishing NuGet packages.
From the pusher-dotnet-client
directory:
- Update
PusherClient/PusherClient.csproj
andPusherClient/Pusher.cs
with new version number etc. - Specify the correct path to
msbuild.exe
inpackage.cmd
and run it. - Run
nuget push PusherClient.{VERSION}.nupkg
This code is free to use under the terms of the MIT license.