Create a .NET client within minutes that connects to
Diffusion™
Cloud
. This example creates a client that prints the value of a
JSON topic to the console when the topic is updated.
To complete this example, you need a
Diffusion
Cloud
service. For more information about getting a
Diffusion
Cloud service,
see Getting started with Diffusion Cloud.
You also require either a named user that has a role with the select_topic and read_topic permissions or that anonymous client connections are assigned a role
with the select_topic and read_topic permissions. For example, the "CLIENT"
role. For more information about roles and permissions, see Role-based authorization.
This example steps through the lines of code required to subscribe to a topic. The full
code example is provided after the steps.
-
Create a .NET project that
references the
DLL
file that can be downloaded from the
following location: Download the .NET
assembly
-
Diffusion.Client
- The assembly contains the interface classes and interfaces that you use when
creating a control client.
The .NET assembly is also available through NuGet.
Use the following
.NET CLI command:
dotnet add package Diffusion.Client -v 6.11.6
-
In your project, create a C# file that uses the following packages:
using System.Threading;
using System.Threading.Tasks;
using PushTechnology.ClientInterface.Client.Callbacks;
using PushTechnology.ClientInterface.Client.Factories;
using PushTechnology.ClientInterface.Client.Features;
using PushTechnology.ClientInterface.Client.Features.Topics;
using PushTechnology.ClientInterface.Client.Topics.Details;
using PushTechnology.ClientInterface.Data.JSON;
using static PushTechnology.ClientInterface.Examples.Runner.Program;
using static System.Console;
namespace PushTechnology.ClientInterface.Example.Consuming {
public sealed class ConsumingJSONTopics : IExample {
}
}
-
Create a Main method.
public sealed class SubscribingClient
{
public static void Main( string[] args ) {
}
}
-
In the Main method, connect to
Diffusion
Cloud
.
public static void Main( string[] args ) {
// Connect anonymously
// Replace 'host' with your hostname
var session = Diffusion.Sessions.Open("ws://host:80");
}
Or you can connect securely, using
Secure Sockets Layer (SSL):
var session = Diffusion.sessions().Open("wss://host:443");
Or you can connect with a principal and credentials if that principal is assigned a
role with the
read_topic permission:
var session = Diffusion.Sessions().Principal("principal")
.Password("password").Open("wss://host:443");
Replace the host, principal, and
password values with your own information.
-
Next, in the Main method, get the Topics feature.
// Get the Topics feature to subscribe to topics
var topics = session.Topics;
The Topics feature enables a client to
subscribe to a topic or fetch its state.
-
Within the subscribing client class, create an inner class that implements
IValueStream for a JSON topic.
This inner class defines the behavior that occurs when a topic that the client
subscribes to is updated. In this example, the topic stream prints the topic path and the
content of the update to the console.
internal sealed class JSONStream : IValueStream<IJSON> {
public void OnClose() {
Console.WriteLine( "The subscription stream is now closed." );
}
public void OnError( ErrorReason errorReason ) {
Console.WriteLine( "An error has occured : {0}", errorReason );
}
public void OnSubscription( string topicPath, ITopicSpecification specification ) {
Console.WriteLine( "Client subscribed to {0} ", topicPath );
}
public void OnUnsubscription( string topicPath, ITopicSpecification specification, TopicUnsubscribeReason reason ) {
Console.WriteLine( "Client unsubscribed from {0} : {1}", topicPath, reason );
}
public void OnValue( string topicPath, ITopicSpecification specification, IJSON oldValue, IJSON newValue ) {
Console.WriteLine( "New value of {0} is {1}", topicPath, newValue.ToJSONString() );
}
}
-
Back in the Main method of the subscribing client class, use the
AddStream method to associate an instance of the topic stream
that you created with the topic you want to subscribe to. In this example, the topic path is "foo/counter".
var topic = ">foo/counter";
// Add a topic stream for 'foo/counter' and request subscription
var jsonStream = new JSONStream();
topics.AddStream( topic, jsonStream );
topics.Subscribe( topic, new TopicsCompletionCallbackDefault() );
-
Use a Thread.sleep() to hold the client open for 10 minutes while the
updates are received and output.
//Stay connected for 10 minutes
Thread.Sleep( TimeSpan.FromMinutes( 10 ) );
session.Close();
-
Compile and run your client.
The client outputs the value to the console every time the value of the foo/counter topic is updated. You can update the value
of the foo/counter topic by using the
Diffusion
Cloud Dashboard's test
client or by creating a publishing client to update the topic. To create and publish
to the foo/counter topic, you require a user with
the modify_topic and update_topic permissions. For more information, see
Start publishing with .NET.
Full example
The completed subscribing client class contains
the following
code: