Giter VIP home page Giter VIP logo

pusher-websocket-dotnet's Introduction

Pusher .NET Client library

This is a .NET library for interacting with the Pusher WebSocket API.

Registering at http://pusher.com and use the application credentials within your app as shown below.

More general documentation can be found at http://pusher.com/docs/.

Installation

NuGet Package

Install-Package PusherClient

Usage

See the example app for full details.

Connect

_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 Error: " + error.ToString());
}

Or 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();

Or 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();

Subscribe to a public or private channel

_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!");
}

Bind to an event

_myChannel.Bind("my-event", (dynamic data) =>
{
    Console.WriteLine(data.message);
});

Subscribe to a presence channel

_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();
}

Unbind

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();

Developer Notes

Publish to NuGet

You should be familiar with creating an publishing NuGet packages.

From the pusher-dotnet-client directory:

  1. Update pusher-dotnet-client.nuspec with new version number etc.
  2. Run package.cmd
  3. Run tools/nuget.exe push PusherClient.{VERSION}.nupkg

License

This code is free to use under the terms of the MIT license.

pusher-websocket-dotnet's People

Contributors

leggetter avatar willsewell avatar boogier avatar imaji avatar mchandschuh avatar zimbatm avatar

Watchers

James Cloos avatar Leonardo Vernaza avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.