Php ZMQ push integration over http

I am trying to implement push integration using php and native zmq. I successfully sent my message to the server, but the problem is that I cannot send the message to the browser using js Websocket (). I say that the connection to WebSocket with "ws: //127.0.0.1: 8080 /" failed: WebSocket handshake error: invalid status bar

here is my code for the client:

<?php try { function send($data) { $context = new ZMQContext(); $push = new ZMQSocket($context, ZMQ::SOCKET_PUSH); $push->connect("tcp://localhost:5555"); $push->send($data); } if(isset($_POST["username"])) { $envelope = array( "from" => "client", "to" => "owner", "msg" => $_POST["username"] ); send(json_encode($envelope)); # send the data to server } } catch( Exception $e ) { echo $e->getMessage(); } ?> 

client

here is my server:

 $context = new ZMQContext(); $pull = new ZMQSocket($context, ZMQ::SOCKET_PULL); $pull->bind("tcp://*:5555"); #this will be my pull socket from client $push = new ZMQSocket($context, ZMQ::SOCKET_PUSH); $push->bind("tcp://127.0.0.1:8080"); # this will be the push socket to owner while(true) { $data = $pull->recv(); # when I receive the data decode it $parse_data = json_decode($parse_data); if($parse_data["to"] == "owner") { $push->send($parse_data["msg"]); # forward the data to the owner } printf("Recieve: %s.\n", $data); } 

and here is my owner.php, I expect the data to be sent via Websocket in the browser:

 <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h2>Message</h2> <ul id="messagelog"> </ul> <script> var logger = document.getElementById("messagelog"); var conn = new WebSocket("ws://127.0.0.1:8080"); # the error is pointing here. conn.onOpen = function(e) { console.log("connection established"); } conn.onMessage = function(data) { console.log("recieved: ", data); } conn.onError = function(e) { console.log("connection error:", e); } conn.onClose = function(e) { console.log("connection closed~"); } </script> </body> 

Please tell me what I am missing. thanks.

+7
javascript php pushstate websocket zeromq
source share
2 answers

You cannot connect the websocket to the zmq * socket, these are different communication protocols (websocket is more like a traditional socket, the zmq socket is more of an abstraction, which adds additional features). You need to configure the method on your server to get a connection to the web server.

* You may be able to do this work using RAW socket types, but this is a bit more advanced and you don’t need to enter if you don’t know what you are doing.

+1
source

You have not connected to the protocol at all. You managed to get the message, but you never confirmed by parsing it and sending the corresponding response that your server is really a WebSocket server.

Since you already use PHP and ZeroMQ, the easiest way is to use Mongrel2 , which, among other things, is capable of understanding the WebSocket protocol and delivering it to the ZeroMQ endpoint encoded as tnetstring (json-like encoding format, trivial for parsing).

Another solution is to fully support the WebSocket protocol in your code - something that goes beyond this question and answer.

+2
source

All Articles