Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions samples/csharp/clientpubsub-client-sdk/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Client pub-sub with `Azure.Messaging.WebPubSub.Client`

## Prerequisites

1. [NET 6 or above](https://docs.microsoft.com/dotnet)
2. Create an Azure Web PubSub resource

## Overview

With client sdk `Azure.Messaging.WebPubSub.Client`, it's easy to communicate among clients and leverage reliable protocol by default to overcome transient connection drop.

## Setup

Copy **Connection String** from **Keys** tab of the created Azure Web PubSub service, and replace the `<connection-string>` below with the value of your **Connection String**.

![Connection String](./../../../docs/images/portal_conn.png)

1. Start Client subscriber

```bash
cd clientsub
dotnet run -- "<ConnectionString>" pubsubhub
```

2. Start Client publisher

```bash
cd clientpub
dotnet run -- "ConnectionString>" pubsubhub
```

Start typing messages and you can see these messages are transferred to the client subscriber in real-time.
46 changes: 46 additions & 0 deletions samples/csharp/clientpubsub-client-sdk/clientpub/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Threading.Tasks;

using Azure.Messaging.WebPubSub;
using Azure.Messaging.WebPubSub.Clients;

namespace clientpub
{
class Program
{
static async Task Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: clientpub <connectionString> <hub>");
return;
}
var connectionString = args[0];
var hub = args[1];

// Either generate the URL or fetch it from server or fetch a temp one from the portal
var serviceClient = new WebPubSubServiceClient(connectionString, hub);

var client = new WebPubSubClient(new WebPubSubClientCredential(async token =>
{
return await serviceClient.GetClientAccessUriAsync(userId: "user1", roles: new string[] { "webpubsub.joinLeaveGroup.demogroup", "webpubsub.sendToGroup.demogroup" });
}));

client.Connected += e =>
{
Console.WriteLine($"Connected: {e.ConnectionId}");
return Task.CompletedTask;
};

await client.StartAsync();

var streaming = Console.ReadLine();
while (streaming != null)
{
await client.SendToGroupAsync("demogroup", BinaryData.FromString(streaming), WebPubSubDataType.Text);
streaming = Console.ReadLine();
}
}
}
}
13 changes: 13 additions & 0 deletions samples/csharp/clientpubsub-client-sdk/clientpub/clientpub.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.WebPubSub" Version="1.3.0" />
<PackageReference Include="Azure.Messaging.WebPubSub.Client" Version="1.0.0-beta.1" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: update version when merge.

</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions samples/csharp/clientpubsub-client-sdk/clientsub/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Threading.Tasks;

using Azure.Messaging.WebPubSub;
using Azure.Messaging.WebPubSub.Clients;

namespace clientsub
{
class Program
{
static async Task Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: clientsub <connectionString> <hub>");
return;
}
var connectionString = args[0];
var hub = args[1];

// Either generate the URL or fetch it from server or fetch a temp one from the portal
var serviceClient = new WebPubSubServiceClient(connectionString, hub);

var client = new WebPubSubClient(new WebPubSubClientCredential(async token =>
{
return await serviceClient.GetClientAccessUriAsync(userId: "user1", roles: new string[] { "webpubsub.joinLeaveGroup.demogroup", "webpubsub.sendToGroup.demogroup" });
}));

client.Connected += e =>
{
Console.WriteLine($"Connected: {e.ConnectionId}");
return Task.CompletedTask;
};

client.GroupMessageReceived += e =>
{
Console.WriteLine($"Message received: {e.Message.Data}");
return Task.CompletedTask;
};

await client.StartAsync();
await client.JoinGroupAsync("demogroup");
Console.Read();
}
}
}
13 changes: 13 additions & 0 deletions samples/csharp/clientpubsub-client-sdk/clientsub/clientsub.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.WebPubSub" Version="1.3.0" />
<PackageReference Include="Azure.Messaging.WebPubSub.Client" Version="1.0.0-beta.1" />
</ItemGroup>

</Project>