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
| 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. |
For the console Quickstart, run from the project directory:
dotnet add package PubnubPCLFor other installation methods, see environment setup.
This example runs in a .NET 6+ console application. It subscribes to hello_world, publishes one message, and prints the received text.
- Open the PubNub Admin Portal.
- Create an app and a keyset for development, or select an existing development keyset.
- 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.
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();
}
}dotnet runThe 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.
| Task | Documentation |
|---|---|
| Configure the client | Configuration |
| Work with subscriptions and messages | Publish & Subscribe |
| Check channel occupancy | Presence |
| Read message history | Message Persistence |
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.
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.
| 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.
Read the changelog before upgrading.
For a major version change, review the C# SDK changelog for breaking changes before upgrading.
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.