Azure Function coded as a PubSub listener works for about 5 minutes, but then seems to go unconscious after 10

Kurt Friedrich 20 Reputation points
2023-06-10T20:27:30.45+00:00

I have a static web app which creates a record and sends it to a Azure Function (http triger) which takes the message, and sends it on to my Azure PubSub instance. I have another function (ListenAndWrite), the one with the problem, that is subscribed to that PubSub. It takes the record in the message and writes it to a Cosmos Mongo DB.

If I run ListenAndWrite locally inside of Code, everything works perfectly, and I can come back 2 hours later and its all still working fine. If instead, I run ListenAndWrite up in Azure, it works for as long as I publish a new record every few minutes, but if I stop sending new data to PuBSub for 10 minutes, ListenAndWrite goes to sleep.

If I just open a browser window and paste in the URL of the ListenAndWrite, it "wakes up" and will again work fine until I again stop activity for 10 minutes.

I run another simple listener just for debug purposes, so I can see PubSub is in fact continuing to work, it is publishing the new records, it is the ListenAndWrite Function that has the problem.

Here is a typical test cycle:

12:37 started and worked

12:39 wait 2 minutes, still works

12:41 wait 2 minutes, still works

12:45 wait 3 minutes, still works

12:50 wait 5 minutes, still works

12:55 wait 5 minutes, still works (18 minutes from when started)

1:05 wait 10 minutes, fails

refreshed the ListenAndWrite URL

1:08 works

1:10 works

1:20 wait 10 minutes, fails

I can't imagine how my ListenAndWrite can subscribe to PubSub with a 5 minute limit??

===================

My guess is that pinging the URL causes main() to be called, which opens the PubSub socket, but then normal Azure management software puts it to sleep when there is no activity for ~5 minutes. Somehow my PubSub trigger code is not correct and waking it back up on PubSub events.

Here is the ListenAndWrite Function 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:jdEpblh blah blah =@kurtcosmosaccount@' );
  return client;
}

async function main() {
  const hub = "kurtpubsub.webpubsub.azure.com";
  var connectionString = "Endpoint=https://kurtpubsub.webpubsub.azure.com;AccessKey=p/  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);  // removes the bad " " from the property names
     // 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
    }
}

exports.index = main;


And here is the function.file

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

host.json file

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

package.json file

{
  "name": "newlistenwritemongo",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "func start",
    "test": "echo \"No tests yet...\""
  },
  "dependencies": {
    "mongodb": "^5.6.0",
    "@azure/web-pubsub": "^1.0.0",
    "@azure/web-pubsub-express": "^1.0.0",
    "ws": "^7.4.4"
  }
}

The End

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
{count} votes