4

I have the following scenario:

I have a given program which provides a JSON interface to which I can connect using a socket connection. Since I want to integrate that interface into my web-application, I'm trying to use the PHP sockets for the communication between server and client. The communication is bidirectional, which means my PHP client is sending requests to the server and the server is also sending requests to my PHP "client". I have no problems with the connection between my PHP and the JSON interface. The only problem is, since I have to wait for requests on the PHP side, I have to run it in an infinity loop. I want to 'echo' some responses and requests i get somewhere into my web-application without having that infinity loop.

My question is, is there a good way to create one php file which can:

  • create an own socket server so I can send stuff to it from my web application without being stuck in an infinity loop
  • the stuff I sent to it can be redirected to the JSON server
  • the response I get from my JSON server redirecting to my web application

Use case I have a solution for: I have a NFC card reader which provides me the functions and informations of a card (uniqueid) and it's connected to my network. The JSON server sends me a request on "card detected" and I respond with "allowed" or "not allowed". (There the infinity loop doesn't matter)

Use case I don't have a solution for: I have my web application open and I want to write the "uniqueid" parameter into an input field to assign that card to a person. I want to do it this way: - Click a button "assign card" - Hold card over the card reader - Write uniqueid into input field

I don't want to make a direct connection from the web application to the JSON server. I want to make a temporary connection from the web application to the PHP server which has a permanent connection to the JSON server.

I hope this is understandable.

Florian Sauerwein
  • 445
  • 1
  • 5
  • 19
  • 1
    Have you looked at websockets ? This would allow you to maintain continious bidirectional socket from your browser to web server. – insanebits Jul 10 '15 at 12:39
  • I was just about to say the same as @insanebits. You might want to checkout this [tutorial](http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket) for more details – tftd Jul 10 '15 at 12:39
  • oh wonderful! that will probably help! i will check it out. thanks! – Florian Sauerwein Jul 10 '15 at 12:58
  • Wait, I have tried to use this before. But it doesn't support tcp sockets. So do you mean I can use it like this: webapp <-> php socket <-> tcp socket (json interface) ? – Florian Sauerwein Jul 10 '15 at 13:01

1 Answers1

1

Yes you can. Look into using Ratchet in your application. It seems to fit your requirements. It has bi-directional communication via Websockets.

Your browser will connect to a Ratchet based application in your server listening in a certain port and you will be able to send and receive messages using that connection.

The alternative is long-polling. You can learn more in this StackOverflow answer (which also features Websockets).

Community
  • 1
  • 1
Ricardo Velhote
  • 4,630
  • 1
  • 24
  • 30