3

Hi I'm trying to send a message to a group using the Azure Signal R Serverless JS Client Js Library.

I can do this from the Azure Serverless Function as simply as:

await signalRMessages.AddAsync(
                new SignalRMessage
                {
                    GroupName = m.GroupName,
                    Target = m.Target,
                    Arguments = new[] { m.Message }
                });

*where signalRMessages = IAsyncCollector signalRMessages

How can I send this same message from the js library?

lopezbertoni
  • 3,551
  • 3
  • 37
  • 53
pmeyer
  • 890
  • 7
  • 31

2 Answers2

2

trying to send a message to a group using the Azure Signal R Serverless

You can refer to this github repo that shows with sample code how to implement group broadcasting functionality in Azure functions with Azure SignalR Service.

Add user to a group using the SignalRGroupAction class

return signalRGroupActions.AddAsync(
    new SignalRGroupAction
    {
        ConnectionId = decodedfConnectionId,
        UserId = message.Recipient,
        GroupName = message.Groupname,
        Action = GroupAction.Add
    });

On client side, make request to endpoint to add a user to a group

function addGroup(sender, recipient, connectionId, groupName) {
        return axios.post(`${apiBaseUrl}/api/addToGroup`, {
          connectionId: connectionId,
          recipient: recipient,
          groupname: groupName
        }, getAxiosConfig()).then(resp => {
          if (resp.status == 200) {
            confirm("Add Successfully")
          }
        });
      }

Test Result

enter image description here

Updated:

Q: "send the message from the JS Client straight from the socket".

A: From here, we can find:

Although the SignalR SDK allows client applications to invoke backend logic in a SignalR hub, this functionality is not yet supported when you use SignalR Service with Azure Functions. Use HTTP requests to invoke Azure Functions.

Fei Han
  • 26,415
  • 1
  • 30
  • 41
  • Thanks! The answer you posted here is how to join a group... I got all of that working as well as sending a message to a group (via HTTP trigger) What I want to do is send the message from the JS Client (straight from the socket) – pmeyer Nov 14 '19 at 12:22
  • `The answer you posted here is how to join a group` If you check the github repo I shared, you would find complete sample code. And [function sendMessage](https://github.com/Azure/azure-functions-signalrservice-extension/blob/dev/samples/simple-chat/content/index.html#L103) could help send message to specific group. – Fei Han Nov 15 '19 at 01:42
  • 1
    `What I want to do is send the message from the JS Client (straight from the socket)` From [here](https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-concept-serverless-development-config#sending-messages-from-a-client-to-the-service), you can find: *"Although the SignalR SDK allows client applications to invoke backend logic in a SignalR hub, this functionality is not yet supported when you use SignalR Service with Azure Functions. Use HTTP requests to invoke Azure Functions."* – Fei Han Nov 15 '19 at 01:57
  • Thanks, seems like it's not supported yet. I have no issues sending using HTTP requests... If you update the answer with this info, Ill mark as the correct answer. – pmeyer Nov 15 '19 at 13:45
0

It's seems like this is now possible ...

https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-concept-serverless-development-config#sending-messages-from-a-client-to-the-service

Sending messages from a client to the service If you have upstream configured for your SignalR resource, you can send messages from client to your Azure Functions using any SignalR client. Here is an example in JavaScript:

JavaScript

connection.send('method1', 'arg1', 'arg2');

pmeyer
  • 890
  • 7
  • 31