title | description | author | ms.topic | ms.custom | ms.date | ms.author |
---|---|---|---|---|---|---|
Azure Functions SignalR Service output binding |
Learn about the SignalR Service output binding for Azure Functions. |
craigshoemaker |
reference |
devx-track-csharp |
02/20/2020 |
cshoe |
Use the SignalR output binding to send one or more messages using Azure SignalR Service. You can broadcast a message to:
- All connected clients
- Connected clients authenticated to a specific user
The output binding also allows you to manage groups.
For information on setup and configuration details, see the overview.
The following example shows a function that sends a message using the output binding to all connected clients. The target is the name of the method to be invoked on each client. The Arguments property is an array of zero or more objects to be passed to the client method.
[FunctionName("SendMessage")]
public static Task SendMessage(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message,
[SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
Target = "newMessage",
Arguments = new [] { message }
});
}
Here's binding data in the function.json file:
Example function.json:
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the C# Script code:
#r "Microsoft.Azure.WebJobs.Extensions.SignalRService"
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
public static Task Run(
object message,
IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
Target = "newMessage",
Arguments = new [] { message }
});
}
Here's binding data in the function.json file:
Example function.json:
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the JavaScript code:
module.exports = async function (context, req) {
context.bindings.signalRMessages = [{
"target": "newMessage",
"arguments": [ req.body ]
}];
};
Here's binding data in the function.json file:
Example function.json:
{
"type": "signalR",
"name": "outMessage",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the Python code:
def main(req: func.HttpRequest, outMessage: func.Out[str]) -> func.HttpResponse:
message = req.get_json()
outMessage.set(json.dumps({
'target': 'newMessage',
'arguments': [ message ]
}))
@FunctionName("sendMessage")
@SignalROutput(name = "$return", hubName = "chat")
public SignalRMessage sendMessage(
@HttpTrigger(
name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Object> req) {
SignalRMessage message = new SignalRMessage();
message.target = "newMessage";
message.arguments.add(req.getBody());
return message;
}
You can send a message only to connections that have been authenticated to a user by setting the user ID in the SignalR message.
[FunctionName("SendMessage")]
public static Task SendMessage(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message,
[SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
// the message will only be sent to this user ID
UserId = "userId1",
Target = "newMessage",
Arguments = new [] { message }
});
}
Example function.json:
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the C# Script code:
#r "Microsoft.Azure.WebJobs.Extensions.SignalRService"
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
public static Task Run(
object message,
IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
// the message will only be sent to this user ID
UserId = "userId1",
Target = "newMessage",
Arguments = new [] { message }
});
}
Example function.json:
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the JavaScript code:
module.exports = async function (context, req) {
context.bindings.signalRMessages = [{
// message will only be sent to this user ID
"userId": "userId1",
"target": "newMessage",
"arguments": [ req.body ]
}];
};
Here's binding data in the function.json file:
Example function.json:
{
"type": "signalR",
"name": "outMessage",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the Python code:
def main(req: func.HttpRequest, outMessage: func.Out[str]) -> func.HttpResponse:
message = req.get_json()
outMessage.set(json.dumps({
#message will only be sent to this user ID
'userId': 'userId1',
'target': 'newMessage',
'arguments': [ message ]
}))
@FunctionName("sendMessage")
@SignalROutput(name = "$return", hubName = "chat")
public SignalRMessage sendMessage(
@HttpTrigger(
name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Object> req) {
SignalRMessage message = new SignalRMessage();
message.userId = "userId1";
message.target = "newMessage";
message.arguments.add(req.getBody());
return message;
}
You can send a message only to connections that have been added to a group by setting the group name in the SignalR message.
[FunctionName("SendMessage")]
public static Task SendMessage(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message,
[SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
// the message will be sent to the group with this name
GroupName = "myGroup",
Target = "newMessage",
Arguments = new [] { message }
});
}
Example function.json:
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the C# Script code:
#r "Microsoft.Azure.WebJobs.Extensions.SignalRService"
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
public static Task Run(
object message,
IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
// the message will be sent to the group with this name
GroupName = "myGroup",
Target = "newMessage",
Arguments = new [] { message }
});
}
Example function.json:
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the JavaScript code:
module.exports = async function (context, req) {
context.bindings.signalRMessages = [{
// message will only be sent to this group
"groupName": "myGroup",
"target": "newMessage",
"arguments": [ req.body ]
}];
};
Here's binding data in the function.json file:
Example function.json:
{
"type": "signalR",
"name": "outMessage",
"hubName": "<hub_name>",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"direction": "out"
}
Here's the Python code:
def main(req: func.HttpRequest, outMessage: func.Out[str]) -> func.HttpResponse:
message = req.get_json()
outMessage.set(json.dumps({
#message will only be sent to this group
'groupName': 'myGroup',
'target': 'newMessage',
'arguments': [ message ]
}))
@FunctionName("sendMessage")
@SignalROutput(name = "$return", hubName = "chat")
public SignalRMessage sendMessage(
@HttpTrigger(
name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Object> req) {
SignalRMessage message = new SignalRMessage();
message.groupName = "myGroup";
message.target = "newMessage";
message.arguments.add(req.getBody());
return message;
}
SignalR Service allows users to be added to groups. Messages can then be sent to a group. You can use the SignalR
output binding to manage a user's group membership.
The following example adds a user to a group.
[FunctionName("addToGroup")]
public static Task AddToGroup(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest req,
ClaimsPrincipal claimsPrincipal,
[SignalR(HubName = "chat")]
IAsyncCollector<SignalRGroupAction> signalRGroupActions)
{
var userIdClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier);
return signalRGroupActions.AddAsync(
new SignalRGroupAction
{
UserId = userIdClaim.Value,
GroupName = "myGroup",
Action = GroupAction.Add
});
}
The following example removes a user from a group.
[FunctionName("removeFromGroup")]
public static Task RemoveFromGroup(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest req,
ClaimsPrincipal claimsPrincipal,
[SignalR(HubName = "chat")]
IAsyncCollector<SignalRGroupAction> signalRGroupActions)
{
var userIdClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier);
return signalRGroupActions.AddAsync(
new SignalRGroupAction
{
UserId = userIdClaim.Value,
GroupName = "myGroup",
Action = GroupAction.Remove
});
}
Note
In order to get the ClaimsPrincipal
correctly bound, you must have configured the authentication settings in Azure Functions.
The following example adds a user to a group.
Example function.json
{
"type": "signalR",
"name": "signalRGroupActions",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"hubName": "chat",
"direction": "out"
}
Run.csx
#r "Microsoft.Azure.WebJobs.Extensions.SignalRService"
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
public static Task Run(
HttpRequest req,
ClaimsPrincipal claimsPrincipal,
IAsyncCollector<SignalRGroupAction> signalRGroupActions)
{
var userIdClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier);
return signalRGroupActions.AddAsync(
new SignalRGroupAction
{
UserId = userIdClaim.Value,
GroupName = "myGroup",
Action = GroupAction.Add
});
}
The following example removes a user from a group.
Example function.json
{
"type": "signalR",
"name": "signalRGroupActions",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"hubName": "chat",
"direction": "out"
}
Run.csx
#r "Microsoft.Azure.WebJobs.Extensions.SignalRService"
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
public static Task Run(
HttpRequest req,
ClaimsPrincipal claimsPrincipal,
IAsyncCollector<SignalRGroupAction> signalRGroupActions)
{
var userIdClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier);
return signalRGroupActions.AddAsync(
new SignalRGroupAction
{
UserId = userIdClaim.Value,
GroupName = "myGroup",
Action = GroupAction.Remove
});
}
Note
In order to get the ClaimsPrincipal
correctly bound, you must have configured the authentication settings in Azure Functions.
The following example adds a user to a group.
Example function.json
{
"type": "signalR",
"name": "signalRGroupActions",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"hubName": "chat",
"direction": "out"
}
index.js
module.exports = async function (context, req) {
context.bindings.signalRGroupActions = [{
"userId": req.query.userId,
"groupName": "myGroup",
"action": "add"
}];
};
The following example removes a user from a group.
Example function.json
{
"type": "signalR",
"name": "signalRGroupActions",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"hubName": "chat",
"direction": "out"
}
index.js
module.exports = async function (context, req) {
context.bindings.signalRGroupActions = [{
"userId": req.query.userId,
"groupName": "myGroup",
"action": "remove"
}];
};
The following example adds a user to a group.
Example function.json
{
"type": "signalR",
"name": "action",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"hubName": "chat",
"direction": "out"
}
__init.py__
def main(req: func.HttpRequest, action: func.Out[str]) -> func.HttpResponse:
action.set(json.dumps({
'userId': 'userId1',
'groupName': 'myGroup',
'action': 'add'
}))
The following example removes a user from a group.
Example function.json
{
"type": "signalR",
"name": "action",
"connectionStringSetting": "<name of setting containing SignalR Service connection string>",
"hubName": "chat",
"direction": "out"
}
__init.py__
def main(req: func.HttpRequest, action: func.Out[str]) -> func.HttpResponse:
action.set(json.dumps({
'userId': 'userId1',
'groupName': 'myGroup',
'action': 'remove'
}))
The following example adds a user to a group.
@FunctionName("addToGroup")
@SignalROutput(name = "$return", hubName = "chat")
public SignalRGroupAction addToGroup(
@HttpTrigger(
name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Object> req,
@BindingName("userId") String userId) {
SignalRGroupAction groupAction = new SignalRGroupAction();
groupAction.action = "add";
groupAction.userId = userId;
groupAction.groupName = "myGroup";
return action;
}
The following example removes a user from a group.
@FunctionName("removeFromGroup")
@SignalROutput(name = "$return", hubName = "chat")
public SignalRGroupAction removeFromGroup(
@HttpTrigger(
name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Object> req,
@BindingName("userId") String userId) {
SignalRGroupAction groupAction = new SignalRGroupAction();
groupAction.action = "remove";
groupAction.userId = userId;
groupAction.groupName = "myGroup";
return action;
}
The following table explains the binding configuration properties that you set in the function.json file and the SignalRConnectionInfo
attribute.
function.json property | Attribute property | Description |
---|---|---|
type | n/a | Must be set to signalRConnectionInfo . |
direction | n/a | Must be set to in . |
name | n/a | Variable name used in function code for connection info object. |
hubName | HubName | This value must be set to the name of the SignalR hub for which the connection information is generated. |
userId | UserId | Optional: The value of the user identifier claim to be set in the access key token. |
connectionStringSetting | ConnectionStringSetting | The name of the app setting that contains the SignalR Service connection string (defaults to "AzureSignalRConnectionString") |
The following table explains the binding configuration properties that you set in the function.json file and the SignalR
attribute.
function.json property | Attribute property | Description |
---|---|---|
type | n/a | Must be set to signalR . |
direction | n/a | Must be set to out . |
name | n/a | Variable name used in function code for connection info object. |
hubName | HubName | This value must be set to the name of the SignalR hub for which the connection information is generated. |
connectionStringSetting | ConnectionStringSetting | The name of the app setting that contains the SignalR Service connection string (defaults to "AzureSignalRConnectionString") |
[!INCLUDE app settings to local.settings.json]