Gopendra
Gopendra

Reputation: 31

How to run ratchet websocket server in parallel with apache server

I'm trying to create a chat application using ratchet and php. I created websocket server to listen on port 8080 in server.php file but I can't find how run it parallel to the apache server after hosting.

server.php

<?php

require 'vendor/autoload.php';

use Ratchet\Server\IoServer;

use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Core\Socket\Chat;
use Core\Router;
use Core\Request;

$server = IoServer::factory(
  new HttpServer(
    new WsServer(
      new Chat()
    )
  ),
  8080
);

$server->run();

index.php

<!DOCTYPE html>
<html>
  <head>
    <title>A Chat App</title>
  </head>
  <body>
    <h1>Chat App</h1>
  </body>
  <script>
    const socket = new WebSocket('ws://localhost:8080');

    socket.addEventListener('open', event => {
      console.log('Connection established');
      socket.send('Hey There Everyone');
    });

    socket.addEventListener('message', message => {
      console.log(message);
    });
  </script>
</html>

This works fine when I run server.php using cmd and index.php on localhost. But how to run these files in parallel after hosting on the web hosting service.

Upvotes: 1

Views: 1532

Answers (1)

jasonwubz
jasonwubz

Reputation: 361

You are running two different processes that want to use the same port. So this setup is not possible. But you can ask apache to serve your websocket script instead of using the command line.

See link to a post that has great details on approaches you can take: Setting up a websocket on Apache?

Upvotes: 2

Related Questions