How to Trigger Node Azure Functions WebPubSub Trigger Binding in for messages from Users locally ?

Smith Johnson 0 Reputation points
2025-02-01T01:39:19.98+00:00

Hi all,

I want to create a serverless Client -> Azure Function for bi-directional communication using Azure Functions with the Azure WebPubSub Trigger binding "in" for user messages. I followed everything in the following tutorial https://learn.microsoft.com/en-us/azure/azure-web-pubsub/quickstart-serverless?tabs=javascript-v3 and am using Node for Azure functions with the Javascript v3 model specified on the linked article. I want to test this locally but the article never specified how this can be actually accomplished they just skip to deploying the function and testing it there...

Here's the basic view for how my client and subscriber look omitting the boilerplate code needed

CLIENT:


   
	let ws = new WebSocket(this.#url);
    ws.onopen = function(){
      ws.send("hello world");
    }
    ws.onmessage = (event) => {
      console.log("NEW MESSAGE");
      console.log({event});
    };

Server (Node 18.x.x):

function.json

{
  "disabled": false,
  "bindings": [
    {
      "type": "webPubSubTrigger",
      "direction": "in",
      "name": "data",
      "hub": "testhub",
      "eventName": "message",
      "eventType": "user"
    }
  ]
}



index.js

module.exports = function (context, data) {
    console.log('Request from: ', context.bindingData.request.connectionContext.userId);
    console.log('Request message data: ', data);
    console.log('Request message dataType: ', context.bindingData.request.dataType);
  }


host.json

{
  "version": "2.0",
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.3.*, 4.0.0)"
  }
}

I've been trying to invoke my trigger binding (folder is named test-user-event if it matters) via the line above

 ws.send("hello world");


But nothing ever hits my local machine... I have checked over multiple issue points and everything I have set up seems correct according the latest documentation...

  1. My connection string variables are correct and point to an active WebPubSub resource I have In Azure
  2. My azure functions works locally when using RESTful endpoints
  3. I correctly connect to the WebSocket and the message Is sent
  4. Both client and server are connected to the same hub
  5. Set up the event handler on that hub to support tunnel:///runtime/webhooks/webpubsub I am aware of the https://www.npmjs.com/package/@azure/web-pubsub-tunnel-tool/v/1.0.0-beta.1 Azure tunnel tool but isnt this only for user when you have a persistent server such as express? How do I accomplish this with Azure functions alone? The WS send is a POST request how Is that supposed to hit my Azure WebPubSub trigger locally?

Also I used the native browser WebSocket implementation for that client but is all this possible using the official Azure SDK as well? https://www.npmjs.com/package/@azure/web-pubsub-client

Azure Web PubSub
Azure Web PubSub
An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
83 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,385 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.