Question

I have a php socket server that I use to handle messages from a modem. I want to create a php websocket server and whenever I receive a message from the modem I send it to a webclient. I'm a couple days trying to do it without success.

How is the best way to do it? Thanks

.

Here is the way I want to do:

Modem ------> PHP Socket Server ------> Web Clients
                    v
                 database

But if you have another best way to do it, tell me.

Thankyou again.

 

 

EDITED: This is an alternative for socket_read errors in non-blocking socket: http://php.net/manual/pt_BR/function.socket-read.php#73509

Was it helpful?

Solution

You can do it with non-blocking socket and long polling technique.

The workflow is something like this.

  1. PHP connects to Modem with a non-blocking socket.
  2. Web client sends a long polling http request which times out after 5 or 10 minutes.
  3. PHP made the incoming client connection as non-blocking. This way PHP adds every client to a queue.
  4. PHP iterates over each of the clients and modem and check if something could be written or read.
  5. if there is something to read from Modem it reads and process. If necessary message is written to Webclient socket.
  6. Same as step 5 but goes from web client socket to modem socket.
  7. Do any additional work.
  8. GO to step 3.

I had a similar situation months ago. I had to keep web sessions in PHP and there was another server which PHP was connected too. Couldn't finished it. Later I had to move to Python for this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top