I'm trying to setup a simple chat app that uses WebSockets in a serverless way. I'm using Azure's WebPubSub service and the code is hosted in an Azure Function App.
I'm using javascript and basing my code in this sample app: https://github.com/Azure/azure-webpubsub/tree/main/samples/functions/js/simplechat That link has all the backend code and all the trigger and bindings are the same as mine.
I can run code on the following events :
- connect
- connected
- message
- disconnect
for the connect event I have the following code: (connect/index.js)
context.bindings.actions.push({
"actionName": "addUserToGroup",
"userId": `${context.bindingData.connectionContext.userId}`,
"group": "group1"
});
that basically tells webPubSub to add the user to a group, in this case called "group1".
For my application, when I send a message, from the client to the socket, I need to know if I'm the only one connected to that group. So far I didn't find anything in the docs regarding this.
In the sample app, whenever there's a message the following code runs (message/index.js)
var response = {
....
"states": {
"counterState": {
counter: msgCounter,
}
}
};
return response;
they increment a msgCounter variable whenever there's a message and save it to the connection state I tried incrementing a variable when the connected event fires but is seems that I cannot set the state in that specific function. I then tried to do that on the message event but it seems that the state is not global for all the connections (i.e. it is specific to a unique connection)
Any help on this would be much apreciated. Thank you