Skip to content

Latest commit

 

History

History
270 lines (185 loc) · 11.3 KB

signalr-quickstart-rest-api.md

File metadata and controls

270 lines (185 loc) · 11.3 KB
titledescriptionauthorms.servicems.topicms.datems.authorms.custom
Quickstart - Azure SignalR Service REST API
Learn how to use REST API with Azure SignalR Service following samples. Find details of REST API specification.
vicancy
azure-signalr-service
quickstart
09/03/2024
lianwei
mode-api

Quickstart: Broadcast real-time messages from console app

Azure SignalR Service provides REST API to support server-to-client communication scenarios such as broadcasting. You can choose any programming language that can make REST API calls. You can post messages to all connected clients, a specific client by name, or a group of clients.

In this quickstart, you learn how to send messages from a command-line app to connected client apps in C#.

[!INCLUDE Connection string security]

Prerequisites

This quickstart can be run on macOS, Windows, or Linux.

[!INCLUDE quickstarts-free-trial-note]

Having issues? Try the troubleshooting guide or let us know.

Sign in to Azure

Sign in to the Azure portal using your Azure account.

Having issues? Try the troubleshooting guide or let us know.

[!INCLUDE Create instance]

Having issues? Try the troubleshooting guide or let us know.

Clone the sample application

While the service is being deployed, let's get the code ready. First, clone the sample app from GitHub. Next, set the SignalR Service connection string to the app. Finally, run the application locally.

  1. Open a git terminal window. Change to a folder where you want to clone the sample project.

  2. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.

    git clone https://github.com/aspnet/AzureSignalR-samples.git

Having issues? Try the troubleshooting guide or let us know.

Build and run the sample

This sample is a console app showing the use of Azure SignalR Service. It provides two modes:

  • Server Mode: use simple commands to call Azure SignalR Service REST API.
  • Client Mode: connect to Azure SignalR Service and receive messages from server.

You also learn how to generate an access token to authenticate with Azure SignalR Service.

Build the executable file

We use macOS osx.10.13-x64 as example. You can find reference on how to build on other platforms.

cd AzureSignalR-samples/samples/Serverless/ dotnet publish -c Release -r osx.10.13-x64

Start a client

[!INCLUDE Connection string security comment]

cd bin/Release/netcoreapp2.1/osx.10.13-x64/ Serverless client <ClientName> -c "<ConnectionString>" -h <HubName>

Start a server

cd bin/Release/netcoreapp2.1/osx.10.13-x64/ Serverless server -c "<ConnectionString>" -h <HubName>

Having issues? Try the troubleshooting guide or let us know.

Run the sample without publishing

You can also run the following command to start a server or client

# Start a server dotnet run -- server -c "<ConnectionString>" -h <HubName># Start a client dotnet run -- client <ClientName> -c "<ConnectionString>" -h <HubName>

Use user-secrets to specify Connection String

You can run dotnet user-secrets set Azure:SignalR:ConnectionString "<ConnectionString>" in the root directory of the sample. After that, you don't need the option -c "<ConnectionString>" anymore.

Having issues? Try the troubleshooting guide or let us know.

Usage

After the server started, use the command to send message:

send user <User Id> send users <User List> send group <Group Name> send groups <Group List> broadcast 

You can start multiple clients with different client names.

Having issues? Try the troubleshooting guide or let us know.

Integration with non-Microsoft services

The Azure SignalR service allows non-Microsoft services to integrate with the system.

Definition of technical specifications

The following table shows all the versions of the REST APIs supported to date. You can also find the definition file for each specific version

VersionAPI StateDoorSpecific
1.0-previewAvailable5002Swagger
1.0AvailableStandardSwagger

The list of available APIs for each specific version is available in the following list.

API1.0-preview1.0
Broadcast to all
Broadcast to a group
Broadcast to some groups (Deprecated)N / A
Send to a user
Send to some users (Deprecated)N / A
Adding a user to a groupN / A
Removing a user from a groupN / A
Check user existenceN / A
Remove a user from all groupsN / A
Send to a connectionN / A
Add a connection to a groupN / A
Remove a connection from a groupN / A
Close a client connectionN / A
Service HealthN / A

Broadcast to everyone

VersionAPI HTTP MethodRequest URLRequest body
1.0-previewPOSThttps://<instance-name>.service.signalr.net:5002/api/v1-preview/hub/<hub-name>{"target": "<method-name>", "arguments": [...]}
1.0POSThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>{"target": "<method-name>", "arguments": [...]}

Broadcast to a group

VersionAPI HTTP MethodRequest URLRequest body
1.0-previewPOSThttps://<instance-name>.service.signalr.net:5002/api/v1-preview/hub/<hub-name>/group/<group-name>{"target": "<method-name>", "arguments": [...]}
1.0POSThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>{"target": "<method-name>", "arguments": [...]}

Sending to a user

VersionAPI HTTP MethodRequest URLRequest body
1.0-previewPOSThttps://<instance-name>.service.signalr.net:5002/api/v1-preview/hub/<hub-name>/user/<user-id>{"target": "<method-name>", "arguments": [...]}
1.0POSThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/users/<user-id>{"target": "<method-name>", "arguments": [...]}

Adding a user to a group

VersionAPI HTTP MethodRequest URL
1.0PUThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>/users/<user-id>

Removing a user from a group

VersionAPI HTTP MethodRequest URL
1.0DELETEhttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>/users/<user-id>

Check user existence in a group

API VersionAPI HTTP MethodRequest URL
1.0GEThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/users/<user-id>/groups/<group-name>
1.0GEThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>/users/<user-id>
Response Status CodeDescription
200User exists
404User not exists

Remove a user from all groups

API VersionAPI HTTP MethodRequest URL
1.0DELETEhttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/users/<user-id>/groups

Send message to a connection

API VersionAPI HTTP MethodRequest URLRequest Body
1.0POSThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/connections/<connection-id>{ "target":"<method-name>", "arguments":[ ... ] }

Add a connection to a group

API VersionAPI HTTP MethodRequest URL
1.0PUThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>/connections/<connection-id>
1.0PUThttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/connections/<connection-id>/groups/<group-name>

Remove a connection from a group

API VersionAPI HTTP MethodRequest URL
1.0DELETEhttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>/connections/<connection-id>
1.0DELETEhttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/connections/<connection-id>/groups/<group-name>

Close a client connection

API VersionAPI HTTP MethodRequest URL
1.0DELETEhttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/connections/<connection-id>
1.0DELETEhttps://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/connections/<connection-id>?reason=<close-reason>

Service Health

API VersionAPI HTTP MethodRequest URL
1.0GEThttps://<instance-name>.service.signalr.net/api/v1/health
Response Status CodeDescription
200Service Good
5xxService Error

Having issues? Try the troubleshooting guide or let us know.

[!INCLUDE Cleanup]

Having issues? Try the troubleshooting guide or let us know.

Next steps

In this quickstart, you learned how to use REST API to broadcast real-time message from SignalR Service to clients. Next, learn more about how to develop and deploy Azure Functions with SignalR Service binding, which is built on top of REST API.

[!div class="nextstepaction"] Develop Azure Functions using Azure SignalR Service bindings

close