A
A
Alexander Razumov2016-04-17 15:25:41
comet
Alexander Razumov, 2016-04-17 15:25:41

How to make websocket work in php without running server via console?

It is necessary to make real time chat in php. I'm thinking of using laravel bootstrap angular and Ratchet library to create real time bi-directional php applications using websocket. The problem is that in the ratchet documentation, when creating such an application, it is necessary to run a script from the console, which, as I understand it, starts the ws server. push-server.php
file :

<?php
    require dirname(__DIR__) . '/vendor/autoload.php';

    $loop   = React\EventLoop\Factory::create();
    $pusher = new MyApp\Pusher;

    // Listen for the web server to make a ZeroMQ push after an ajax request
    $context = new React\ZMQ\Context($loop);
    $pull = $context->getSocket(ZMQ::SOCKET_PULL);
    $pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
    $pull->on('message', array($pusher, 'onBlogEntry'));

    // Set up our WebSocket server for clients wanting real-time updates
    $webSock = new React\Socket\Server($loop);
    $webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
    $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
                new Ratchet\Wamp\WampServer(
                    $pusher
                )
            )
        ),
        $webSock
    );

    $loop->run();

It needs to be run from the command line:
$ php bin/push-server.php
But I don't understand how it should be run on a hosted production server? Everything works on the local machine when you start the server from the console, but how to do this on a hosting is not clear? And you can make it so that you do not have to run it manually all the time for the websocket to work.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2016-04-17
@Denormalization

We just set up the same supervisord that will start / restart the process.

A
Alexey Skobkin, 2016-04-17
@skobkin

but how to do it on hosting is not clear

If by hosting you mean shared hosting for PHP sites, then:
1. They usually provide access via ssh.
2. If you have something more serious than a regular site (and web sockets are more serious) - it's better to rent a VPS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question