Skip to content

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

@javascriptures2

Description

@javascriptures2

Hello,

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 (JS):

    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 message data: ', data);
     }

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

My connection string variables are correct and point to an active WebPubSub resource I have In Azure

  1. My azure functions works locally when using RESTful endpoints
  2. I correctly connect to the WebSocket and the message Is sent
  3. Both client and server are connected to the same hub
  4. 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 Azure tunnel tool but isn't this only for when you have a persistent server such as Express? How do I accomplish this with Azure functions alone? The Client WebSocket sent method 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 via the SendEvent() method? https://www.npmjs.com/package/@azure/web-pubsub-client

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions