Saad Nawaz
Saad Nawaz

Reputation: 231

Sending message to Group isn't working in Azure SignalR

I am working on Azure SignalR with Azure Functions. The broadcast scenario is working perfectly fine; however, sending message to a group isn't working. Following is the code on Azure Functions side:

Following is the negotiate method

@FunctionName("negotiate")
public SignalRConnectionInfo negotiate(
        @HttpTrigger(
            name = "req",
            methods = { HttpMethod.POST },
            authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> req,
        @SignalRConnectionInfoInput(
            name = "connectionInfo",
            hubName = "chat") SignalRConnectionInfo connectionInfo) {

    return connectionInfo;
}

@FunctionName("subscribeToGroup")
@SignalROutput(name = "$return", hubName = "chat")
public SignalRGroupAction subscribeToGroup(
    @HttpTrigger(
        name = "req",
        methods = { HttpMethod.POST },
        authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> req) throws Exception {
    Map<String, String> requestBody = new ObjectMapper().readValue(req.getBody().get(), new TypeReference<HashMap<String, String>>() {});
    return new SignalRGroupAction("add", "groupName", requestBody.get("userId")); //userId is the connectionId sent from client side
}

then trigger for sending message to group:

@FunctionName("sendMessageToChannel")
@SignalROutput(name = "$return", hubName = "chat")
public SignalRMessage sendMessageToChannel(
    @HttpTrigger(
        name = "req",
        methods = { HttpMethod.POST },
        authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> req) throws Exception {

    Map<String, String> messageContainer = new HashMap<>();
    messageContainer.put("sender", "1234");
    messageContainer.put("text", "Hello Group13");

    SignalRMessage message = new SignalRMessage("groupChannel", messageContainer);
    message.groupName = "groupName";

    return message;
}

on the client side; I am listening to the groupChannel target; i.e.

connection.on('groupChannel', function(message) {
    console.log(message);
    $("#groupMessages").append(message.text + "<br/>");
});

not sure what am i doing wrong here. for broadcast it works perfectly fine. The subscribeToGroup method also doesn't throw any exception, same for sendMessageToChannel. it doesn't throw any exception but client doesn't get the message

Upvotes: 3

Views: 411

Answers (1)

Saad Nawaz
Saad Nawaz

Reputation: 231

Thanks to @Thiago Custodio.. it worked. The issue was I wasn't sending userId as part of the negotiate. After making following change; it worked:

@FunctionName("negotiate")
public SignalRConnectionInfo negotiate(
        @HttpTrigger(
            name = "req",
            methods = { HttpMethod.POST },
            authLevel = AuthorizationLevel.ANONYMOUS,
            route = "{userId}/negotiate") HttpRequestMessage<Optional<String>> req,
        @SignalRConnectionInfoInput(
            name = "connectionInfo",
            hubName = "chat",
            userId = "{userId}") SignalRConnectionInfo connectionInfo) {

    return connectionInfo;
}

Upvotes: 4

Related Questions