C# API & SDK Docs 8.0.3
This guide walks you through a simple "Hello, World" application that demonstrates the core concepts of PubNub:
- Setting up a connection
- Sending messages
- Receiving messages in real-time
Overview
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 packages
PubNub 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.
Prerequisites
Before we dive in, make sure you have:
- A basic understanding of C#
- A C# development environment (Visual Studio, Visual Studio Code, Rider)
- .NET Framework 4.6.1+ or .NET Core 2.0+ installed
- A PubNub account (we'll help you set this up!)
Setup
Get your PubNub keys
First, get your PubNub keys:
- Sign in or create an account on the PubNub Admin Portal.
- Create an app (or use an existing one).
- Find your publish and subscribe keys in the app dashboard.
When you create an app, PubNub automatically generates a keyset. You can use the same keyset for development and production, but we recommend separate keysets for each environment to improve security and management.
Install the SDK
SDK version
Always 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
1# For .NET Core / .NET Standard / Xamarin
2dotnet add package PubnubPCL
3
4# For .NET Framework 4.6.1+
5dotnet add package Pubnub
6
7# For Universal Windows Platform (UWP)
8dotnet add package PubnubUWP
Package Manager console in Visual Studio
1# For .NET Core / .NET Standard / Xamarin
2Install-Package PubnubPCL
3
4# For .NET Framework 4.6.1+
5Install-Package Pubnub
6
7# For Universal Windows Platform (UWP)
8Install-Package PubnubUWP
Source code
Clone the GitHub repository:
1git clone https://github.com/pubnub/c-sharp
Steps
Initialize PubNub
You'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.