I need to make requests from the server to application running on hardware at the users home. I can't let the application push the data to the server since i don't store the data, but need to forward it to a client-facing web app (Also i am not allowed to store the data on the server). I am currently unsure how to achieve this. My only idea is to have idle socket connections open where i can write the requests into. This seems complicated and i don't know whether it is scalable (is there a limit on open socket connections? How much resources do idle connections waste?). It is also a bit low-level, but i can not see how i can use http and i don't know other suitable protocols.
Here are some of the options available. I'm assuming that the home network is a typical home installation where incoming connections from the internet are blocked by the home router/firewall by default.
Home Application Polls Server
Since the application running on the home network can connect out to the internet, you could turn the connection around so that the application on the home network connects out to your server on the internet. Depending upon the application and needs, the home application could make a simple HTTP connection to your server, once a day, once an hour, once every 5 minutes, etc... depending upon the situation. If the server has nothing for the home application, then it just returns a simple response that it has no data. If the server has some data for the home application, then it just returns that data when the incoming request comes in.
The main drawback of this scheme is that the server must wait until the next client polling interval before it can deliver data to the client.
Home Application Connects webSocket or socket.io connection the Server
Again, the home application connects to the server, but this time it creates a webSocket or socket.io connection. That connection can then be held open for as long as you like. Once this connection is open, the server is free to send the client data at any time. If the connection is interrupted at any time, the client just re-establishes a new connection. This costs pretty much nothing on the client side and a very small amount of resources on the server (a little memory per connection).
Properly configured servers can handle hundreds of thousands of simultaneous webSocket connections. Specially configured servers can even handle millions of connections.
Open Hole in Home Router/Firewall
In this case, the home router/firewall is configured to "port forward" incoming requests on a specific port (probably not port 80) to the home application. In this case, the home application must have previous registered its IP address with the server and have properly configured the home router/firewall to permit direct access on a specific port to the home application. Once that is all configured properly, the server can connect directly to the home application.
Because regular consumer end-users will often have trouble configuring the router/firewall to permit this access and troubleshooting is difficult, this is not a very commonly selected option.
Hybrid Polling
There is also a hybrid approach where the home client regularly sends a UDP packet to the server. This UDP packet causes the home router/firewall to open up a return path from that same server back to the home client. That return path will stay open for some period of time and during that time interval the server can send a UDP packet to the client and the router/firewall will let it in. This is a derivation of a scheme used by some peer-to-peer protocols in order to get through home routers/firewalls. Because this is UDP, not TCP the client/server are responsible for their own delivery reliability.
来源:https://stackoverflow.com/questions/36484813/how-to-make-request-from-server-to-devices-behind-router