Giter Club home page Giter Club logo

c-sharp's Introduction

PubNub C# SDK

This is the official PubNub C# SDK repository.

PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.

Get keys

You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.

Configure PubNub

  1. Integrate PubNub into your project using one of the following NuGet packages:

  2. Create a new class and add the following code:

    using PubnubApi;
    
    
    PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"));
    pnConfiguration.SubscribeKey = "mySubscribeKey";
    pnConfiguration.PublishKey = "myPublishKey";
    Pubnub pubnub = new Pubnub(pnConfiguration);

    This is the minimum configuration you need to send and receive messages with PubNub.

Add event listeners

pubnub.AddListener(new SubscribeCallbackExt(
    delegate (Pubnub pnObj, PNMessageResult<object> pubMsg)
    {
        if (pubMsg != null) {
            Debug.WriteLine("In Example, SubscribeCallback received PNMessageResult");
            Debug.WriteLine("In Example, SubscribeCallback messsage channel = " + pubMsg.Channel);
            Debug.WriteLine("In Example, SubscribeCallback messsage channelGroup = " + pubMsg.Subscription);
            Debug.WriteLine("In Example, SubscribeCallback messsage publishTimetoken = " + pubMsg.Timetoken);
            Debug.WriteLine("In Example, SubscribeCallback messsage publisher = " + pubMsg.Publisher);
            string jsonString = pubMsg.Message.ToString();
            Dictionary<string, string> msg = pubnub.JsonPluggableLibrary.DeserializeToObject<Dictionary<string,string>>(jsonString);
            Debug.WriteLine("msg: " + msg["msg"]);
        }
    },
    delegate (Pubnub pnObj, PNPresenceEventResult presenceEvnt)
    {
        if (presenceEvnt != null) {
            Debug.WriteLine("In Example, SubscribeCallback received PNPresenceEventResult");
            Debug.WriteLine(presenceEvnt.Channel + " " + presenceEvnt.Occupancy + " " + presenceEvnt.Event);
        }
    }
)

Publish and subscribe

In this code, publishing a message is triggered when the subscribe call is finished successfully. The Publish() method uses the msg variable that is used in the following code.

Dictionary<string, string> message = new Dictionary<string, string>();
    message.Add("msg", "Hello world!");

pubnub.Subscribe<string>()
        .Channels(new string[]{
            "my_channel"
        }).Execute();

PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
                                            .Message(message)
                                            .Channel("my_channel")
                                            .ExecuteAsync();
PNPublishResult publishResult = publishResponse.Result;
PNStatus status = publishResponse.Status;
Console.WriteLine("pub timetoken: " + publishResult.Timetoken.ToString());
Console.WriteLine("pub status code : " + status.StatusCode.ToString());

Documentation

API reference for C#

Support

If you need help or have a general question, contact [email protected].

c-sharp's People

Contributors

budgetpreneur avatar crimsonred avatar marceloinacio avatar parfeon avatar maxpresman avatar mohitpubnub avatar messagingmoore avatar client-engineering-bot avatar mikedobrzan avatar davidnub avatar xavrax avatar techwritermat avatar seba-aln avatar stephenlb avatar musashiwasajedi 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.