NodeJS - Socket.io questions
Hello,
I am currently building a project in nodejs with an angular application as a frontend. I need to send data from my website to the server (POST reqeust) which then emtis data to one of many raspberry PIs and gives feedback to my angular app per websocket connection.
I have a few questions on how to do this whole process:
I have an index.js file where I load all my express middleware etc, and some router files for specific routes. The websocket connection should be initialised outside the router so I dont open a new one every time. Should I create an on connection listener in the index.js file and outsource all the emits to a different file, or should the whole socket server live inside a different file?
I know that socketio needs an http server to work, but I cannot pass an exress server in its constructor. Should I use an http server for my server instead and use express as a request handler? Or should I create a new http server for my websocket connection?
Should I create a different socket server for the server/angular and raspberry/server communication (I will need to store the socketID and token/id of the raspberry in a map to know which raspberry I should send the data to) or should I use the same server with different rooms?
I apologise if those questions are too basic, I have never really worked with websockets so I dont know about the best practices when working with them. I want to do everything as clean as possible.
Original Link: https://dev.to/littleboss/nodejs-socketio-questions-3gjd
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To