How to map signal connection to a user ID? JavaScript serverless

164 views Asked by At

I'm using a serverless JavaScript function app with a default negotiate function and another httpTrigger function called sendMessage.

I'm trying to get sendMessage to send signalR messages to a specific client that has a userId (Broadcasting already works. I want to send to a single user with an id). How can I map signalR connections with a user id in the client app to be able to send messages to him later?

The client connects to the hub in the browser like this:

const connection = new signalR.HubConnectionBuilder()
             .withUrl(`${apiBaseUrl}/api`)
             .build();

connection.on('newMessage', (msg)=>{
  console.log(msg)
  }
);
connection.start()
  .catch(console.error);
  /////////////////////////////////////////////////////////////////
  // do something here to like connection with userId??

What I need to do with my azure function is to send a message to a specific user with userId. Like this:

module.exports = async function (context, req) {
    context.bindings.signalRMessages  = [{
        // message will only be sent to this user ID
        "userId": "userId1",
        "target": "newMessage",
        "arguments": ["some message to userId1"]
    }];
};

How can I map userIds to connections when the user connects to the hub? The documentation has an example of doing this but they use azure app services authentication. I don't want to authenticate users. I already have an authentication method in my app. I just need to link userIds to connections when they connect to a hub.

0

There are 0 answers