pushpin icon indicating copy to clipboard operation
pushpin copied to clipboard

php get refused port

Open thehatami opened this issue 5 years ago • 10 comments

after fresh installation and test, i try to send msg to channel from php. here is my code

$pub = new GripPubControl(array(
            'control_uri' => 'http://localhost:7779'
        ));

        $pub->publish_http_stream('ws', 'hello there\n');

but i got this error even open port in firewall.

Failed to publish: Failed to connect to localhost port 7779: Connection refused

whats wrong?

thehatami avatar Jun 15 '19 17:06 thehatami

By default, Pushpin’s control port is 5561, so double check you’re specifying the correct port.

jkarneges avatar Jun 15 '19 17:06 jkarneges

thx jkarneges. but when i use this port on my client side i get

Can’t establish a connection to the server at ws://localhost:5561/ws.

why?

thehatami avatar Jun 15 '19 18:06 thehatami

Clients connect to a front port (7999 by default) not the control port.

The reason for multiple ports is because the control port is private, to be used from the server side only.

jkarneges avatar Jun 15 '19 18:06 jkarneges

sorry. but how connect backend(php) to frontend(js)? my code not work. why?

frontend

var ws = new WebSocket('ws://localhost:7999/ws');
ws.onmessage = function (message) {
            console.log(message.data);
};

backend

$pub = new GripPubControl(array(
            'control_uri' => 'http://localhost:5561'
));

$pub->publish_http_stream('ws', 'hello there\n');

thehatami avatar Jun 15 '19 19:06 thehatami

Where is /ws routing to? If it's the test handler, you need to publish to channel test. If it's routing to your backend server, then your server needs to send a subscription control message.

jkarneges avatar Jun 15 '19 20:06 jkarneges

Also, publish_http_stream publishes data only to clients that are using the HTTP streaming transport.

To publish to clients that are connected with the WebSocket transport, you need to publish a websocket message like this:

$pub->publish('test', new PubControl\Item(
    new GripControl\WebSocketMessageFormat('hello there')
));

jkarneges avatar Jun 15 '19 20:06 jkarneges

thx. you help is very helpful. i think documents need more clear specially in php+js programming. and the last question is how can create new channel name. any channel except 'ws' with 'test' publish not work. i read route route section but cant find my answer.

thehatami avatar Jun 15 '19 20:06 thehatami

See https://pushpin.org/docs/usage/#websockets . There's a PHP example.

jkarneges avatar Jun 15 '19 20:06 jkarneges

thx but im confused. to create new channel i used below code

<?php
// composer require fanout/gripcontrol

$in_events = GripControl::decode_websocket_events(
    file_get_contents("php://input"));
$out_events = array();
if ($in_events[0]->type == 'OPEN')
{
    $out_events[] = new WebSocketEvent('OPEN');
    $out_events[] = new WebSocketEvent('TEXT', 'c:' .
        GripControl::websocket_control_message('subscribe',
        array('channel' => 'mychannel')));
}

header('Content-Type: application/websocket-events');
header('Sec-WebSocket-Extensions: grip');
http_response_code(200);

echo GripControl::encode_websocket_events($out_events);

?>

but i get Notice: Undefined offset: 0

how can i create new channel and how assign handler to it?

thehatami avatar Jun 15 '19 21:06 thehatami

I'm guessing that error is coming from $in_events[0] ? This script is meant to be invoked by Pushpin, in which case events will be provided and the code should be ok.

Try making a request directly to your php script using curl. You should see something like this:

$ curl -i -d 'OPEN'$'\r'$'\n' http://localhost:8000/yourphpfile.php
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 22:23:31 GMT
Server: Apache/2.4.29 (Ubuntu)
Sec-WebSocket-Extensions: grip
Content-Length: 61
Content-Type: application/websocket-events

OPEN
TEXT 2c
c:{"channel":"mychannel","type":"subscribe"}

If you don't see that, then something is wrong with the script. But if you do see that, and you have Pushpin configured to route to your backend server, then making a WebSocket request through Pushpin (e.g. to ws://localhost:7999/yourphpfile.php) should work.

jkarneges avatar Jun 15 '19 22:06 jkarneges