Reputation: 156
I am developing a project that is including :
Iot devices: each device is configured with the ip and port of the TCP server, once is successfully connected, the iot device will send information like its ip and port, IMEI as a unique identifier.
TCP server: This server is on listening of any connection from the iot devices. It's already reachable from a fixed ip and Port.
This TCP server is a nodesjs base project and i am using net package to handle socket connection of the iot devices.
A mongodb Database where I am storing the iot device information like IMEI, port, ip
HTTP Server: This server expose a post endpoint that will be called from a mobile phone to send command to do something or to request device information e.g. battery level, gps location etc.
This Post endpoint will require IMEI and command as params, then i fetch in db to get ip and port of the iot device to use it later to establish a socket connection.
My question is about TCP connection: If the TCP server is getting a connection from iot device then a socket connection is opened, then the data will be received and saved in the database.
Do I have to keep the socket alive in order to send commands later or not?
I have tried to open socket connection to the iot device using its ip and port received one it connected first time to TCP server, but I couldn't connect. a timeout error is returned.
Do i have to store the socket it self, i have read that is not possible to store sockets.
Does my approaches correct?
Note that I will have many devices that are connected similarly to the TCP server.
Upvotes: 0
Views: 651
Reputation: 707876
Do I have to keep the socket alive in order to send commands later or not?
Yes, you do. If you want to send from the server to the IOT device, then you will need a live TCP connection and assuming the IOT device is a client-only, that means the client has to maintain the connection to the server.
I have tried to open socket connection to the iot device using its ip and port received one it connected first time to TCP server, but I couldn't connect. a timeout error is returned.
Unless your IOT device is itself a server (e.g. listening for incoming connections), you can't connect directly to it from the server.
Do i have to store the socket it self, i have read that is not possible to store sockets.
Not sure what you mean here. A socket is essentially a "handle" to an open connection, somewhat analogous to a file handle when you open a file. Both are only useful as long as the underlying resource is still open.
You have two architectural styles you could go with here.
IOT devices are a server
You could make each IOT device its own little server and have it listening for an incoming connection on a known port and IP address. That way, when the main server wants to send something to the IOT device, it can just make a connection to it directly and then send it whatever it wants to send it. When the operation is complete, your main server can then close the connection.
This has the advantage that no continuous connections need to be maintained, but it may have practical issues with your server knowing the IP address and port for every IOT device. There could also be security considerations if anyone on the same network can connect to the IOT device to tell it to do something.
Maintain continuous connection from each IOT device to server
Here's the IOT device can just remain a client. As soon as it is powered up, it will make a connection to the main server and maintain that connection. This will involve sending regular keep-alive traffic to the server and the server responding back such that the socket doesn't time out and such that the client can detect if the socket seems to have gone dead and needs to be dropped and reconnected.
A library such as socket.io (socket.io protocol running on a webSocket which is running on TPC) will do this for you (you also make your main server a socket.io server instead of just a TCP server and you make your IOT device a socket.io client).
Upvotes: 0