javascriptsocketcluster

Send message to socketcluster channel from outside worker


There is an external resource I am watching and whenever the data changes I would like to send a message to a specific channel depending on the data.

I found two ways to do this by bonking my head repeatedly against the SC docs:

How can I do this from within server.js?


Solution

  • A good example I have of sending data or a message when an external resource is changed, is for that external resource (like a website) to call your socket cluster on/emit through the standard way:

    socket.on('documents/upload-file', function (data, respond) {
        documents.uploadFile(data, respond, socket);
    });
    

    socket.exchange.publish('channels/documentList', updatedDocumentList);

    The whole point of a worker is to handle external incoming events. Without knowing what your external resource is then I can't specifically say if this is the best approach for you, but hopefully it's a good place to start with.