In this sample, we demonstrate how to broadcast messages with SignalR Service and Azure Function in serverless.
Because ESM support is still in preview for Azure Functions, this quickstart uses CommonJS modules including node-fetch. If you update the node-fetch package to 3.x, you may need to update the code to use ESM modules, which requires changing from const
to import
and *.js
to *.mjs
.
Start local storage emulator in terminal.
npm run start:azurite
Rename
local.settings.template.json
tolocal.settings.json
and update env variable.SIGNALR_CONNECTION_STRING
: The connection string of your Azure SignalR Service.
Run command to start Azure Function locally in different terminal.
npm start
Visit
http://localhost:7071/api/index
.Go to this repo's home page and click the star button to increase or decrease the star count.
The Azure Functions broadcast function is triggered every minute to see if the star count has changed. If it has chanaged base on checking the eTag, a new star count is sent as a message to the client.