3
votes

I'm working on a project where I need to send data (not much, ~300B(+-100) at once) every 5 minutes. On one side is a raspberry pi with apache2 server and an additional PHP script which serves as a TCP socket server.

On the other side, there are multiple clients, which connect to TCP socket server. Connection is established by the server, they exchange the data and then the socket is closed. This happens every 5 minutes per client (so if I have 100 clients, the socket Will be opened and closed 20 times a minute).

Is this a good way to handle such connections, or should I do it somehow differently? Are PHP scripts fine?

2

2 Answers

0
votes

It's either this or connecting each client once and keeping the connection alive. With the numbers you're specifying both are valid choices, but a socket is a resource and the OS can't really allocate new resources ad infinitum. Therefore, it will have to reuse them, so this approach of opening and closing is one that scales well since currently unused sockets can be recycled.

Now on the other hand, if your server is running apache2 for the sole reason of running a PHP script which only serves as a TCP socket server then I'd say this is not the best way to make this. You can probably use WebSockets (still using PHP) and run php via the CLI and not need to have apache2 running.

0
votes

So if I read this right, you have remote clients that are opening a TCP Connection to a PHP app hosted on an Apache HTTP Server running on the raspberry Pi 2. A client will connect to the server every 5 minutes and send about 300 bytes (+/- 100 bytes) of data. So, that's a TCP connection opened every 5 second. This is nothing for the Raspberry Pi unless it take a huge number of CPU to process the 300 bytes you get.

I would not recommend to try to keep the network connection alive for 5 minutes (either using TCP keep-alive and/or HTTP keep-live and/or using web sockets) since the load is so low, you will put more stress on the system trying to keep 100 connections alive than to let them re-connect every 5 minutes.