Azure Function PubSub Listener works, even though it show errors when starting

Kurt Friedrich 20 Reputation points
2023-06-08T20:42:21.0833333+00:00

This is a follow on to a prior question: https://learn.microsoft.com/en-us/answers/questions/1298419/serverless-function-works-when-run-local-fails-whe

The answer to that problem was to not use an HTTP trigger, but to instead use a PubSub trigger. I recoded the Azure Function and now when I start it, it throws some ugly errors, but then works. I'd like to get rid of these startup errors.

The Function subscribes to my PubSub, and when it gets a message (with a new record object) it writes it to my Cosmos MongoDB.

here is the code:

const WebSocket = require('ws');
const { WebPubSubServiceClient } = require('@azure/web-pubsub');

const MongoClient = require('mongodb').MongoClient;
const connect = async () => {
  client = await MongoClient.connect('mongodb://kurtcosmosaccount: blah blah blah .mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@kurtcosmosaccount@' );
  return client;
}

async function main() {
  const hub = "kurtpubsub.webpubsub.azure.com";
  var connectionString = "Endpoint=https://kurtpubsub.webpubsub.azure.com;AccessKey=blah blah =;Version=1.0;";
  let serviceClient = new WebPubSubServiceClient(connectionString, hub);
  let token = await serviceClient.getClientAccessToken();
  let ws = new WebSocket(token.url);
  ws.on('open', () => console.log('connected kurt'));
  ws.on('message', data => {
    let newRecord = JSON.parse(data);
     // got the data, now write to mongo
     // have to wait for 2nd promise to get the client object
     connect().then( console.log("connected to mongo"))
     .then((client) => { writeDB(client, newRecord)});
     console.log(newRecord);
   });
}

main();

function writeDB(client, pNewRecord)
{
  try {
    const database = client.db("restdb");
    console.log("connected to DB successfully");
      // Insert a single document
      
      database.collection('restaurants').insertOne(pNewRecord)
      .then(console.log("mongo did not complain"));
      //client.close();
    } catch (err) {
      console.log(err.stack);
      client.close();     // Close connection if app is dying
    }
}

Here is the function.json file

{
  "disabled": false,
  "bindings": [
    {
      "type": "webPubSubTrigger",
      "direction": "in",
      "name": "data",
      "hub": "kurtpubsub.webpubsub.azure.com",
      "eventName": "message",
      "eventType": "user"
    }
  ]
}

Here is the error:

[2023-06-08T20:37:13.330Z] Worker was unable to load function ListenThenWriteDB: 'Unable to determine function entry point. If multiple functions are exported, you must indicate the entry point, either by naming it 'run' or 'index', or by naming it explicitly via the 'entryPoint' metadata property.'

[2023-06-08T20:37:13.338Z] Worker failed to load function: 'ListenThenWriteDB' with functionId: 'a85a6965-5577-4f36-9fdb-e1ae0489a4c6'.

[2023-06-08T20:37:13.340Z] Result: Failure

Here is the host.json file

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  }
}
Azure Web PubSub
Azure Web PubSub
An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
60 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,284 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kurt Friedrich 20 Reputation points
    2023-06-10T00:50:19.3266667+00:00

    After pushing to Azure, then with the project open in Code, I selected the command from the F1 pallet "Azure Functions: Download Remote Settings"

    and now it starts with no error.

    0 comments No comments