|
| 1 | +# Azure Web PubSub service client library for .NET |
| 2 | + |
| 3 | +[Azure Web PubSub Service](https://aka.ms/awps/doc) is a service that enables you to build real-time messaging web applications using WebSockets and the publish-subscribe pattern. Any platform supporting WebSocket APIs can connect to the service easily, e.g. web pages, mobile applications, edge devices, etc. The service manages the WebSocket connections for you and allows up to 100K **concurrent** connections. It provides powerful APIs for you to manage these clients and deliver real-time messages. |
| 4 | + |
| 5 | +Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients, can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests, can also use Azure Web PubSub service. |
| 6 | + |
| 7 | +This library can be used to do the following actions. Details about the terms used here are described in [Key concepts](#key-concepts) section. |
| 8 | + |
| 9 | +- Send messages to hubs and groups. |
| 10 | +- Send messages to particular users and connections. |
| 11 | +- Organize users and connections into groups. |
| 12 | +- Close connections |
| 13 | +- Grant, revoke, and check permissions for an existing connection |
| 14 | + |
| 15 | +[Source code](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/webpubsub/Azure.Messaging.WebPubSub/src) | |
| 16 | +Package TBD | |
| 17 | +[API reference documentation](https://aka.ms/awps/sdk/csharp) | |
| 18 | +[Product documentation](https://aka.ms/awps/doc) | |
| 19 | +[Samples][samples_ref] |
| 20 | + |
| 21 | +## Getting started |
| 22 | + |
| 23 | +### Install the package |
| 24 | + |
| 25 | +Install the client library from [NuGet](https://www.nuget.org/): |
| 26 | + |
| 27 | +```PowerShell |
| 28 | +dotnet add package Azure.Messaging.WebPubSub --prerelease |
| 29 | +``` |
| 30 | + |
| 31 | +### Prerequisites |
| 32 | + |
| 33 | +- An [Azure subscription][azure_sub]. |
| 34 | +- An existing Azure Web PubSub service instance. |
| 35 | + |
| 36 | +### Authenticate the client |
| 37 | + |
| 38 | +In order to interact with the service, you'll need to create an instance of the WebPubSubServiceClient class. To make this possible, you'll need the connection string or a key, which you can access in the Azure portal. |
| 39 | + |
| 40 | +### Create a `WebPubSubServiceClient` |
| 41 | + |
| 42 | +```csharp |
| 43 | +var serviceClient = new WebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>")); |
| 44 | +``` |
| 45 | + |
| 46 | +## Key concepts |
| 47 | + |
| 48 | +### Connection |
| 49 | + |
| 50 | +Connections, represented by a connection id, represent an individual websocket connection to the Web PubSub service. Connection id is always unique. |
| 51 | + |
| 52 | +### Hub |
| 53 | + |
| 54 | +Hub is a logical concept for a set of connections. Connections are always connected to a specific hub. Messages that are broadcast to the hub are dispatched to all connections to that hub. Hub can be used for different applications, different applications can share one Azure Web PubSub service by using different hub names. |
| 55 | + |
| 56 | +### Group |
| 57 | + |
| 58 | +Group allow broadcast messages to a subset of connections to the hub. You can add and remove users and connections as needed. A client can join multiple groups, and a group can contain multiple clients. |
| 59 | + |
| 60 | +### User |
| 61 | + |
| 62 | +Connections to Web PubSub can belong to one user. A user might have multiple connections, for example when a single user is connected across multiple devices or multiple browser tabs. |
| 63 | + |
| 64 | +### Message |
| 65 | + |
| 66 | +Using this library, you can send messages to the client connections. A message can either be string text, JSON or binary payload. |
| 67 | + |
| 68 | +## Examples |
| 69 | + |
| 70 | +### Broadcast a text message to all clients |
| 71 | + |
| 72 | +```csharp |
| 73 | +var serviceClient = new WebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>")); |
| 74 | +await serviceClient.SendToAll("Hello world!"); |
| 75 | +``` |
| 76 | + |
| 77 | +### Broadcast a JSON message to all clients |
| 78 | + |
| 79 | +```csharp |
| 80 | +var serviceClient = new WebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>")); |
| 81 | +await serviceClient.SendToAll( |
| 82 | + RequestContent.Create( |
| 83 | + new { |
| 84 | + Foo = "Hello World!", |
| 85 | + Bar = 42 |
| 86 | + })); |
| 87 | +``` |
| 88 | + |
| 89 | +### Broadcast a binary message to all clients |
| 90 | + |
| 91 | +```csharp |
| 92 | +var serviceClient = new WebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", new AzureKeyCredential("<access-key>")); |
| 93 | + |
| 94 | +client.SendToAll( |
| 95 | + RequestContent.Create(new byte[] {0x1, 0x2, 0x3}), |
| 96 | + HttpHeader.Common.OctetStreamContentType.Value |
| 97 | +); |
| 98 | +``` |
| 99 | + |
| 100 | +## Troubleshooting |
| 101 | + |
| 102 | +### Setting up console logging |
| 103 | +You can also easily [enable console logging](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Diagnostics.md#logging) if you want to dig deeper into the requests you're making against the service. |
| 104 | + |
| 105 | +## Next steps |
| 106 | + |
| 107 | +Please take a look at the |
| 108 | +[samples][samples_ref] |
| 109 | +directory for detailed examples on how to use this library. |
| 110 | + |
| 111 | +## Contributing |
| 112 | + |
| 113 | +This project welcomes contributions and suggestions. |
| 114 | +Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. |
| 115 | +For details, visit <https://cla.microsoft.com.> |
| 116 | + |
| 117 | +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). |
| 118 | +Simply follow the instructions provided by the bot. |
| 119 | +You will only need to do this once across all repos using our CLA. |
| 120 | + |
| 121 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 122 | +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +[azure_sub]: https://azure.microsoft.com/free/ |
| 127 | +[samples_ref]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/ |
0 commit comments