question

AaronManill-1369 avatar image
0 Votes"
AaronManill-1369 asked ryanchill commented

Can node JS recieve signalR messages without a frontend?

I am looking to implement an automated printing service where a system running nodeJS can be notified from serverless SignalR that a file can now be retrieved for printing. Is this something that is achievable? From the documentation I've been able to find so far on javascript based signalR it looks like almost all of the signalR data processing is being done on a client end, and that nodeJS is just being used to actually point the client to the signalR service.

azure-signalr-serviceazure-web-pubsub
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered

I think using a Web PubSub instance would be a better fit. It encapsulates SignalR service into a simplified publisher/receiver model. This tutorial walks you through creating a simple publisher and subscriber app.

// * publisher * //

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

const hub = "pubsub";
let service = new WebPubSubServiceClient(process.env.WebPubSubConnectionString, hub);

// by default it uses `application/json`, specify contentType as `text/plain` if you want plain-text
service.sendToAll(process.argv[2], { contentType: "text/plain" });


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

async function main() {
  const hub = "pubsub";
  let service = new WebPubSubServiceClient(process.env.WebPubSubConnectionString, hub);
  let token = await service.getClientAccessToken();
  let ws = new WebSocket(token.url);
  ws.on('open', () => console.log('connected'));
  ws.on('message', data => console.log('Message received: %s', data));
}

main();

Your publisher can be an Azure Function that's either triggered through HTTP, Timer, or a Queue and use a webPubSub as an output to broadcast and any connected subscriber will receive the message. This serverless notification tutorial demonstrates how to set this up. Your subscriber should be able to run headless but using node, you would still need to run it locally.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.