|
| 1 | +--- |
| 2 | +title: Azure SignalR service data plane REST API reference |
| 3 | +description: Describes the REST APIs Azure SignalR service supports to manage the connections and send messages to them. |
| 4 | +author: vicancy |
| 5 | +ms.author: lianwei |
| 6 | +ms.service: signalr |
| 7 | +ms.topic: reference |
| 8 | +ms.date: 06/09/2022 |
| 9 | +--- |
| 10 | + |
| 11 | +# Azure SignalR service data plane REST API reference |
| 12 | + |
| 13 | +> [!NOTE] |
| 14 | +> |
| 15 | +> Azure SignalR Service only supports using REST API to manage clients connected using ASP.NET Core SignalR. Clients connected using ASP.NET SignalR use a different data protocol that is not currently supported. |
| 16 | +
|
| 17 | +On top of the classical client-server pattern, Azure SignalR Service provides a set of REST APIs so that you can easily integrate real-time functionality into your server-less architecture. |
| 18 | + |
| 19 | +<a name="serverless"></a> |
| 20 | + |
| 21 | +## Typical Server-less Architecture with Azure Functions |
| 22 | + |
| 23 | +The following diagram shows a typical server-less architecture using Azure SignalR Service with Azure Functions. |
| 24 | + |
| 25 | +:::image type="content" source="./media/signalr-reference-data-plane-rest-api/serverless-arch.png" alt-text="Diagram of a typical serverless architecture for Azure SignalR service"::: |
| 26 | + |
| 27 | +- `negotiate` function returns a negotiation response and redirects all clients to SignalR Service. |
| 28 | +- `broadcast` function calls SignalR Service's REST API. Then SignalR Service will broadcast the message to all connected clients. |
| 29 | + |
| 30 | +In a server-less architecture, clients still have persistent connections to the SignalR Service. |
| 31 | +Since there's no application server to handle traffic, clients are in `LISTEN` mode, which means they can only receive messages but can't send messages. |
| 32 | +SignalR Service will disconnect any client that sends messages because it's an invalid operation. |
| 33 | + |
| 34 | +You can find a complete sample of using SignalR Service with Azure Functions at [here](https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/RealtimeSignIn). |
| 35 | + |
| 36 | +## API |
| 37 | + |
| 38 | +The following table shows all versions of REST API we have for now. You can also find the swagger file for each version of REST API. |
| 39 | + |
| 40 | +API Version | Status | Port | Doc | Spec |
| 41 | +---|---|---|---|--- |
| 42 | +`1.0` | Latest | Standard | [Doc](./swagger/signalr-data-plane-rest-v1.md) | [swagger](https://github.com/Azure/azure-signalr/blob/dev/docs/swagger/v1.json) |
| 43 | +`1.0-preview` | Obsolete | Standard | [Doc](./swagger/signalr-data-plane-rest-v1-preview.md) | [swagger](https://github.com/Azure/azure-signalr/blob/dev/docs/swagger/v1-preview.json) |
| 44 | + |
| 45 | +The latest available APIs are listed as following. |
| 46 | + |
| 47 | + |
| 48 | +| API | Path | |
| 49 | +| ---- | ---------- | |
| 50 | +| [Broadcast a message to all clients connected to target hub.](./swagger/signalr-data-plane-rest-v1.md#broadcast-a-message-to-all-clients-connected-to-target-hub) | `POST /api/v1/hubs/{hub}` | |
| 51 | +| [Broadcast a message to all clients belong to the target user.](./swagger/signalr-data-plane-rest-v1.md#broadcast-a-message-to-all-clients-belong-to-the-target-user) | `POST /api/v1/hubs/{hub}/users/{id}` | |
| 52 | +| [Send message to the specific connection.](./swagger/signalr-data-plane-rest-v1.md#send-message-to-the-specific-connection) | `POST /api/v1/hubs/{hub}/connections/{connectionId}` | |
| 53 | +| [Check if the connection with the given connectionId exists](./swagger/signalr-data-plane-rest-v1.md#check-if-the-connection-with-the-given-connectionid-exists) | `GET /api/v1/hubs/{hub}/connections/{connectionId}` | |
| 54 | +| [Close the client connection](./swagger/signalr-data-plane-rest-v1.md#close-the-client-connection) | `DELETE /api/v1/hubs/{hub}/connections/{connectionId}` | |
| 55 | +| [Broadcast a message to all clients within the target group.](./swagger/signalr-data-plane-rest-v1.md#broadcast-a-message-to-all-clients-within-the-target-group) | `POST /api/v1/hubs/{hub}/groups/{group}` | |
| 56 | +| [Check if there are any client connections inside the given group](./swagger/signalr-data-plane-rest-v1.md#check-if-there-are-any-client-connections-inside-the-given-group) | `GET /api/v1/hubs/{hub}/groups/{group}` | |
| 57 | +| [Check if there are any client connections connected for the given user](./swagger/signalr-data-plane-rest-v1.md#check-if-there-are-any-client-connections-connected-for-the-given-user) | `GET /api/v1/hubs/{hub}/users/{user}` | |
| 58 | +| [Add a connection to the target group.](./swagger/signalr-data-plane-rest-v1.md#add-a-connection-to-the-target-group) | `PUT /api/v1/hubs/{hub}/groups/{group}/connections/{connectionId}` | |
| 59 | +| [Remove a connection from the target group.](./swagger/signalr-data-plane-rest-v1.md#remove-a-connection-from-the-target-group) | `DELETE /api/v1/hubs/{hub}/groups/{group}/connections/{connectionId}` | |
| 60 | +| [Check whether a user exists in the target group.](./swagger/signalr-data-plane-rest-v1.md#check-whether-a-user-exists-in-the-target-group) | `GET /api/v1/hubs/{hub}/groups/{group}/users/{user}` | |
| 61 | +| [Add a user to the target group.](./swagger/signalr-data-plane-rest-v1.md#add-a-user-to-the-target-group) | `PUT /api/v1/hubs/{hub}/groups/{group}/users/{user}` | |
| 62 | +| [Remove a user from the target group.](./swagger/signalr-data-plane-rest-v1.md#remove-a-user-from-the-target-group) | `DELETE /api/v1/hubs/{hub}/groups/{group}/users/{user}` | |
| 63 | +| [Remove a user from all groups.](./swagger/signalr-data-plane-rest-v1.md#remove-a-user-from-all-groups) | `DELETE /api/v1/hubs/{hub}/users/{user}/groups` | |
| 64 | + |
| 65 | +## Using REST API |
| 66 | + |
| 67 | +### Authenticate via Azure SignalR Service AccessKey |
| 68 | + |
| 69 | +In each HTTP request, an authorization header with a [JSON Web Token (JWT)](https://en.wikipedia.org/wiki/JSON_Web_Token) is required to authenticate with SignalR Service. |
| 70 | + |
| 71 | +#### Signing Algorithm and Signature |
| 72 | + |
| 73 | +`HS256`, namely HMAC-SHA256, is used as the signing algorithm. |
| 74 | + |
| 75 | +Use the `AccessKey` in Azure SignalR Service instance's connection string to sign the generated JWT token. |
| 76 | + |
| 77 | +#### Claims |
| 78 | + |
| 79 | +The following claims are required to be included in the JWT token. |
| 80 | + |
| 81 | +Claim Type | Is Required | Description |
| 82 | +---|---|--- |
| 83 | +`aud` | true | Needs to be the same as your HTTP request url, trailing slash and query parameters not included. For example, a broadcast request's audience should look like: `https://example.service.signalr.net/api/v1/hubs/myhub`. |
| 84 | +`exp` | true | Epoch time when this token expires. |
| 85 | + |
| 86 | +### Authenticate via Azure Active Directory Token (Azure AD Token) |
| 87 | + |
| 88 | +Similar to authenticating using `AccessKey`, when authenticating using Azure AD Token, a [JSON Web Token (JWT)](https://en.wikipedia.org/wiki/JSON_Web_Token) is also required to authenticate the HTTP request. |
| 89 | + |
| 90 | +The difference is, in this scenario the JWT Token is generated by Azure Active Directory. |
| 91 | + |
| 92 | +[Learn how to generate Azure AD Tokens](/azure/active-directory/develop/reference-v2-libraries) |
| 93 | + |
| 94 | +You could also use **Role Based Access Control (RBAC)** to authorize the request from your client/server to SignalR Service. |
| 95 | + |
| 96 | +[Learn how to configure Role-based access control roles for your resource](/azure/azure-signalr/authorize-access-azure-active-directory) |
| 97 | + |
| 98 | +### Implement Negotiate Endpoint |
| 99 | + |
| 100 | +As shown in the [architecture section](#serverless), you should implement a `negotiate` function that returns a redirect negotiation response so that client can connect to the service. |
| 101 | +A typical negotiation response looks as follows: |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "url":"https://<service_name>.service.signalr.net/client/?hub=<hub_name>", |
| 106 | + "accessToken":"<a typical JWT token>" |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +The `accessToken` is generated using the same algorithm described in [authentication section](#authenticate-via-azure-signalr-service-accesskey). The only difference is the `aud` claim should be same as `url`. |
| 111 | + |
| 112 | +You should host your negotiate API in `https://<hub_url>/negotiate` so you can still use SignalR client to connect to the hub url. |
| 113 | + |
| 114 | +Read more about redirecting client to Azure SignalR Service at [here](./signalr-concept-internals.md#client-connections). |
| 115 | + |
| 116 | +### User-related REST API |
| 117 | + |
| 118 | +In order to call user-related REST API, each of your clients should identify itself to SignalR Service. |
| 119 | +Otherwise SignalR Service can't find target connections from a given user ID. |
| 120 | + |
| 121 | +Client identification can be achieved by including a `nameid` claim in each client's JWT token when they're connecting to SignalR Service. |
| 122 | +Then SignalR Service will use the value of `nameid` claim as the user ID of each client connection. |
| 123 | + |
| 124 | +### Sample |
| 125 | + |
| 126 | +You can find a complete console app to demonstrate how to manually build a REST API HTTP request in SignalR Service [here](https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/Serverless). |
| 127 | + |
| 128 | +You can also use [Microsoft.Azure.SignalR.Management](<https://www.nuget.org/packages/Microsoft.Azure.SignalR.Management>) to publish messages to SignalR Service using the similar interfaces of `IHubContext`. Samples can be found [here](<https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/Management>). For more information, see [How to use Management SDK](https://github.com/Azure/azure-signalr/blob/dev/docs/management-sdk-guide.md). |
| 129 | + |
| 130 | + |
| 131 | +## Limitation |
| 132 | + |
| 133 | +Currently, we have the following limitation for REST API requests: |
| 134 | + |
| 135 | +* Header size is a maximum of 16 KB. |
| 136 | +* Body size is a maximum of 1 MB. |
| 137 | + |
| 138 | +If you want to send message larger than 1 MB, use the Management SDK with `persistent` mode. |
0 commit comments