I have an Azure PubSub service with an event handler configured using Azure Functions.
With a basic web socket connection from a React app I can connect, send messages, and trigger events in Azure Functions.
Client side JS:
const ws = new WebSocket("http://myservice.azure.com");
ws.onopen = () => {
ws.send("message");
};
Azure Functions:
[FunctionName("NotificationConnected")]
public static void Connected(
ILogger logger,
[WebPubSubTrigger("notification", WebPubSubEventType.System, "connected")] ConnectedEventRequest request,
BinaryData data,
WebPubSubDataType dataType)
{
logger.LogInformation("Request from: {user}, data: {data}, dataType: {dataType}",
request.ConnectionContext.UserId, data?.ToString(), dataType);
}
[FunctionName("NotificationMessage")]
public static async void NotificationMessage(
ILogger logger,
[WebPubSubTrigger("notification", WebPubSubEventType.User, "message")] UserEventRequest request,
BinaryData data,
WebPubSubDataType dataType)
{
logger.LogInformation("Request from: {user}, data: {data}, dataType: {dataType}",
request.ConnectionContext.UserId, data.ToString(), dataType);
}
The above all works as expected and both functions fire. I want to enable group level messaging using the json.webpubsub.azure.v1
sub protocol so on the client side I now have:
const handleMessage = (message: any) => {
const container = JSON.parse(message);
if (container.ackId === 1) {
ws.send(JSON.stringify({
"type": "sendToGroup",
"group": state.group,
"ackId": ++ackId,
"dataType": "text",
"data": "group message"
}));
}
}
const ws = new WebSocket("http://myservice.azure.com", "json.webpubsub.azure.v1");
ws.onopen = () => {
ws.send(JSON.stringify({
type: 'joinGroup',
group: state.group,
ackId: ++ackId
}));
};
ws.onmessage = function (e) {
handleMessage(e.data);
};
In Azure PubSub I can see the client connecting to the group and sending messages to the group, and the 'connected' function still fires in Azure Functions but the 'message' function doesn't.
How can I bind to the joinGroup and sendToGroup events now in Azure Functions?