3

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

Saad Nawaz
  • 221
  • 1
  • 9
  • 1
    Can you post the negotiate function too? I was looking on another java sample, take a look on it too. Maybe it can help you: https://github.com/anthonychu/captionr/blob/b56c96beb0baba24fec932592ec4b3a43ac1c783/src/functions-java/src/main/java/captionr/Operations.java – Thiago Custodio Mar 31 '20 at 21:15
  • here's another sample: https://github.com/Azure/azure-functions-signalrservice-extension/blob/228bab65731cff4e9aa1463fa9c8c5a81082353c/samples/simple-chat/java/src/main/java/com/functions/Functions.java – Thiago Custodio Mar 31 '20 at 21:19
  • Thanks for the reply; sure; updated with negotiate method as well – Saad Nawaz Mar 31 '20 at 21:38
  • 3
    seems to me the problem is that you're not passing the user Id in the negotiate function – Thiago Custodio Mar 31 '20 at 21:43
  • but I won't have connectionId before negotiate completes.. – Saad Nawaz Mar 31 '20 at 21:43
  • you are right this worked.. thanks sooooo much :) – Saad Nawaz Mar 31 '20 at 21:48

1 Answers1

4

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;
}
Saad Nawaz
  • 221
  • 1
  • 9