In this guide, we'll create a simple "Hello, World" application that demonstrates the core concepts of PubNub:
This guide will help you get up and running with PubNub in your C# application, covering environments like .NET Framework, .NET Core, Xamarin, etc.
The core PubNub concepts and API usage are consistent across different C# platforms.
SDK packagesPubNub provides different NuGet packages optimized for various .NET environments:
Pubnub
: For .NET Framework 4.6.1+PubnubPCL
: For .NET Standard 2.0+ (includes .NET Core, Xamarin)PubnubUWP
: For Universal Windows Platform Choose the package that best suits your target platform during installation.Before we dive in, make sure you have:
First things first – you'll need your PubNub keys to get started. Here's how to get them:
When you create a new app, PubNub automatically generates your first set of keys. While you can use the same keys for development and production, we recommend creating separate keysets for each environment for better security and management.
Install the SDK SDK versionAlways use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
To integrate PubNub into your project using the NuGet package manager, choose the command relevant to your project type:
.NET CLI
dotnet add package PubnubPCL
dotnet add package Pubnub
dotnet add package PubnubUWP
Package Manager console in Visual Studio
Install-Package PubnubPCL
Install-Package Pubnub
Install-Package PubnubUWP
You can also get the source code directly from GitHub.
Steps Initialize PubNubYou'll need to initialize the PubNub client with your unique keys to establish a connection to the PubNub network. This is the minimum configuration required to send and receive messages with PubNub in your application.
In a standard C# application (like a console app or backend service), you typically initialize PubNub at the application's entry point or within a dedicated service class.
Make sure to replace the demo keys with your app's publish and subscribe keys from the Admin Portal
This code configures and initializes PubNub within the Main
method of a console application.
For more information, refer to the Configuration section of the SDK documentation.
Set up event listenersListeners allow your application to react to incoming messages and other PubNub events in real-time.
Add the SetupListeners
method to your App
class. In a console or backend application, listeners might log messages or trigger other business logic.
For more information, refer to the Listeners section of the SDK documentation.
Create a subscriptionSubscribing tells PubNub that you want to receive messages published to specific channels.
Add the SubscribeToChannel
method to your App
class. We call this in Main()
after setting up listeners.
Publishing sends messages to a specific channel for all subscribed clients to receive.
A message can be any type of JSON-serializable data (such as objects, arrays, integers, strings) that is smaller than 32 KiB.
Add the PublishMessageAsync
method to your App
class. We call this from Main()
in our example. Using async/await
is common in C# for I/O operations.
Now it's time to see your application in action!
dotnet build
).dotnet run
or execute the .exe
file).Here's the expected output in the console:
PubNub Initialized!
PubNub Listeners Set Up.
Subscribed to channel: my_channel
Message Published! Timetoken: 16788864001234567 // Timetoken will vary
Connected to PubNub on channel(s): my_channel
Message Received: Channel=my_channel, Message={"text":"Hello from C# Console!"}
Parsed Text: Hello from C# Console!
Press any key to exit...
Complete example
Here's the complete working example that puts everything together.
TroubleshootingIf you don't see the expected output, here are some common issues and how to fix them:
Issue Possible Solutions No connection messageSubscribe
call was executed after listeners were added.using PubnubApi;
statements are present.Great job! 🎉 You've successfully created your first PubNub C# application. Here are some exciting things you can explore next:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4