Skip to content
pubnubPublic

About

PubNub clients for C-Sharp based languages, including MS Windows C#/.net, Silveright, IIS, and Mono

Resources

Stars

109 stars

Watchers

40 watching

Forks

Latest commit

 

History

1,787 Commits

Folders and files

Repository files navigation

csharp-header

PubNub C# SDK

NuGet

PubNub provides global infrastructure for real-time, interactive applications.

Publish and receive messages in C#. Use this SDK for .NET applications compatible with the current PubNub C# packages.

Documentation · API reference · Changelog · Support

Requirements

Requirement Supported version or setup
.NET .NET 6.0 or .NET Standard 2.0 for PubnubPCL
Target framework Frameworks supported by the current PubNub NuGet packages
NuGet Install the PubNub package compatible with the application's target framework.

Installation

For the console Quickstart, run from the project directory:

dotnet add package PubnubPCL

For other installation methods, see environment setup.

Quickstart

This example runs in a .NET 6+ console application. It subscribes to hello_world, publishes one message, and prints the received text.

Get your keys

  1. Open the PubNub Admin Portal.
  2. Create an app and a keyset for development, or select an existing development keyset.
  3. Copy its publish key and subscribe key.

If Access Manager is enabled, obtain a token from your trusted backend and call pubnub.SetAuthToken(token). Keep the secret key on the backend.

Send and receive a message

Replace Program.cs with:

Replace the key placeholders. Use a User ID that identifies the user or device in your app.

using System;
using System.Threading.Tasks;
using PubnubApi;

class Program
{
    private static readonly TaskCompletionSource<bool> Received =
        new TaskCompletionSource<bool>();

    static async Task Main()
    {
        PNConfiguration config =
            new PNConfiguration(new UserId("hello-world-user"))
            {
                SubscribeKey = "YOUR_SUBSCRIBE_KEY",
                PublishKey = "YOUR_PUBLISH_KEY"
            };

        Pubnub pubnub = new Pubnub(config);

        SubscribeCallbackExt listener =
            new SubscribeCallbackExt(
                (pn, message) =>
                {
                    Console.WriteLine(message.Message);
                    Received.TrySetResult(true);
                },
                (pn, presence) => { },
                (pn, status) =>
                {
                    if (status.Category ==
                        PNStatusCategory.PNConnectedCategory)
                    {
                        _ = pubnub.Publish()
                            .Channel("hello_world")
                            .Message("Hello world")
                            .ExecuteAsync();
                    }
                }
            );

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

        await Received.Task;

        pubnub.UnsubscribeAll<string>();
        pubnub.RemoveListener(listener);
        pubnub.Destroy();
    }
}

Run the example

dotnet run

The terminal should show:

Hello world

The example calls pubnub.UnsubscribeAll<string>(), pubnub.RemoveListener(listener), and pubnub.Destroy() after receiving the message.

For a complete application, see the getting started guide.

Next steps

Task Documentation
Configure the client Configuration
Work with subscriptions and messages Publish & Subscribe
Check channel occupancy Presence
Read message history Message Persistence

Build with an AI coding assistant

The PubNub MCP server gives an AI coding assistant access to PubNub SDK documentation and PubNub APIs. Connect the assistant to the hosted server at https://mcp.pubnub.com, or run npx @pubnub/mcp@latest locally.

The server repository has setup steps for VS Code, Cursor, Claude Code, Claude Desktop, Codex, Gemini CLI, and OpenCode.

Before production

Use Access Manager to grant each client the permissions it needs. Keep the secret key on your backend. Never include it in a distributed client.

Reuse the PubNub client for the application/service lifetime. Call pubnub.UnsubscribeAll<string>() when its subscriptions should stop, remove listeners with pubnub.RemoveListener(listener), and call pubnub.Destroy() when the client is no longer needed.

Live delivery through PubNub SDKs is at-most-once. A subscriber can miss messages while disconnected or if its buffer overflows. For longer-gap recovery, see Message Persistence.

Troubleshooting

Symptom Check
The project cannot resolve PubnubApi Install the PubNub NuGet package compatible with the target framework and add using PubnubApi;. See the C# SDK guide.
A publish succeeds but no message appears Check the message handler, subscription readiness, keyset, and channel name.
The application targets a framework incompatible with the installed PubNub package Verify the target-framework compatibility of the selected package; PubnubPCL targets .NET 6.0 and .NET Standard 2.0. See NuGet.
Extra clients or duplicate messages during development For ASP.NET or dependency-injection applications, register and reuse a PubNub client at application/service scope rather than constructing one on every request or development reload.

For setup help, see troubleshooting. Check network status for service incidents.

Releases

Read the changelog before upgrading.

For a major version change, review the C# SDK changelog for breaking changes before upgrading.

Support and contributions

For setup or account help, contact PubNub Support.

For a reproducible SDK bug, use GitHub Issues. Include the SDK version, runtime, and a small reproduction with credentials removed.

Restore dependencies, build the affected targets, run the repository test suite, and include tests for behavioral changes before opening a pull request.

License

See the PubNub Software Development Kit License.

About

PubNub clients for C-Sharp based languages, including MS Windows C#/.net, Silveright, IIS, and Mono

Resources

Stars

109 stars

Watchers

40 watching

Forks

Releases

Packages

Used by

Contributors

Languages