I have a server running expressjs that hosts the admin website and 4 to 10 additional worker servers that run a job scheduler and are constantly processing jobs and receiving new data. Is there a good way for the worker servers to notify the website server and then update the data on the website using websockets?
-
sure socket.io works serverside if that's what your asking, they could connect to the *admin* server (or 127.0.0.1 etc if on single server and/or use eventemitter if started on same process) and that could emit events to the ui – Lawrence Cherone Sep 20 '20 at 00:45
-
@LawrenceCherone but how do I send the update from one of the worker servers to the web sever and then to the browser? – Dev01 Sep 20 '20 at 00:47
-
each worker server would connect to the admin, exactly the same as the browser clients, the only thing you would need figure out is identifying each worker but that could be on the connect via a param passed up – Lawrence Cherone Sep 20 '20 at 00:50
-
@LawrenceCherone ok thanks. So servers can connect to servers using websockets? Do any of the libraries, ws, socketio, etc. have better support for that? – Dev01 Sep 20 '20 at 01:25
1 Answers
Worker servers can use any number of means for communicating with your main server. For example, you main server could have a special web server in your main server (not exposed to the outside world) that just receives http requests with data from the workers. So, whenever a worker gets new data, it just sends an http request to the main server.
Or, either webSocket or socket.io can be used server-to-server just fine (one end serves as the client for establishing the connection) and the other is the webSocket/socket.io server listening for incoming connections. In your case, the workers would be the clients and they would connection to your main server and then they can send data whenever they want over the webSocket or socket.io connection.
If you then want to automatically update data in various web browsers that have one of your pages open, those pages would also have a webSocket or socket.io connection open to your server so when your server gets new data, it could then tell the web page about it and the webpage could update its display without constantly refreshing/reloading a new page.
Either ws or socket.io can be used. Socket.io offers a number of features built on top of webSocket. You would choose socket.io if you want those added features. You can see a partial list of the added socket.io features here:

- 683,504
- 96
- 985
- 979