Easiest Way to Send JSON from Server to Client (Arduino) on Network

I am working on a project where Arduino Clients (~40 of them) will be sending data to a REST API server (Node.js, Express.js) via HTTP POSTs containing JSON data. This will be on a network dedicated to facilitating this interaction. The bulk of the information sent on this network will be these HTTP POSTs from these Arduino clients to the REST server.

I do need a way for the server to send information to the Arduino clients. The data is JSON. I could either send an array of size 40 containing the JSON to each client, and then the clients could access their particular element (object) within the array. Alternatively I could send each client its object directly. Whichever is simpler (requires less debugging) is the one I will prefer.

So, I could do this using websockets. The clients would connect to the server's in with websockets using https://github.com/Links2004/arduinoWebSockets library. Then, the server using [express-ws - npm](http://this NPM package)), would send out JSON via websockets.

My gut feeling is that this will be very finicky and require a lot of debugging. So, I thought I'd run it by the Arduino forums before starting.

In short, is websockets the best method for me to send JSON data from server to client?

Sidenote: If I am using websockets to send the JSON from server to client, should I use websockets to send the JSON data from client to server, instead of using HTTP POSTs?

If the ArduinoClient is already posting http-data to your XxxServer - why not give back information to the ArduinoClient in the httpResponse?

Do you really need information sent from your XxxServer to the ArduinoClient asycronally to the active posts?

P.S.: two points:

  • I don't see the fancy thing on websocket so far for your usecase. It might be you should be more specific on message-size/response-size, message-intervall and broadcast-size and broadcast-interval...

  • I hope you have already done some experiments with Json libraries on the Arduino and you are aware of the limited memory you have available on them. I'm not saying that it isn't possible - but you have to care about the device limits. By the way "Arduino" is very unspecific ... what targets are you using on client side, what's your server?

Have you looked at MQTT?

Its a publish-subscribe network protocol that transports messages and runs on top of TCP/IP. It needs a MQTT broker that will handle the messages and provides functionality that will help with many nodes. For instance you can send a message that will be received by many nodes even if some nodes are sleeping.

MQTT is supported on Arduino and there are some nice tools support with Eclipse Paho which allows you to send and receive messages for testing/debugging. Its also supported on Node-RED which being based on JS has nice JSON support.