A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.pubnub.com/docs/c-sharp-dot-net-c-sharp/pubnub-c-sharp-sdk below:

C# API & SDK Docs 7.4.1

C# API & SDK Docs 7.4.1

In this guide, we'll create a simple "Hello, World" application that demonstrates the core concepts of PubNub:

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:

Prerequisites

Before we dive in, make sure you have:

Setup Get your PubNub keys

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 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

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 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

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 listeners

Listeners 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 subscription

Subscribing 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.

Publish messages

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.

Run the app

Now it's time to see your application in action!

  1. Build your C# project (e.g., dotnet build).
  2. Run the compiled application (e.g., dotnet run or execute the .exe file).
  3. Observe the console output. You should see messages indicating initialization, connection, subscription, and the results of any publish operations.
  4. If you publish a message, you should see the "Message Received" output shortly after in the same console (since it's subscribed to the channel it's publishing to).
  5. Run a second instance of the application to see messages published by one instance appearing in the other.
  6. Press any key in the console window to exit the application cleanly.

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.

Troubleshooting

If you don't see the expected output, here are some common issues and how to fix them:

Issue Possible Solutions No connection message Message not received Build/Compile errors Next steps

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