Im am trying to implement push integration using php and native zmq. I have successfully send send my message to server, but my problem is I cannot push the message to browser using js Websocket(). I says WebSocket connection to 'ws://127.0.0.1:8080/' failed: Error during WebSocket handshake: Invalid status line
here is my code for 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'm expecting the data to be send thru Websocket in 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 do tell me what I am missing. thank you.
You didn't establish a protocol communication at all. You managed to receive the message, but you never confirmed, by parsing it and sending appropriate response, that your server is indeed a WebSocket server.
Since you are already using PHP and ZeroMQ, the easiest way out is to use Mongrel2 which is, among other things, capable of understanding WebSocket protocol and deliver it to a ZeroMQ endpoint encoded as a tnetstring (a json-like encoding format, trivial to parse).
The other solution is to fully support the WebSocket protocol in your code - something that's outside of the scope of this question and answer.
You cannot connect a websocket to a zmq socket*, they are different communication protocols (a websocket is more like a traditional socket, a zmq socket is more of an abstraction that adds extra features). You need to set up a way on your server to receive a websocket connection.
*You may be able to make this work using RAW socket types, but that's a bit more advanced and shouldn't be entered into unless you know what you're doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With