Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run WebSocket and HTTP Server on same port #771

Open
maliknaik16 opened this issue Dec 28, 2019 · 8 comments
Open

Unable to run WebSocket and HTTP Server on same port #771

maliknaik16 opened this issue Dec 28, 2019 · 8 comments
Labels

Comments

@maliknaik16
Copy link

maliknaik16 commented Dec 28, 2019

I've the following code:

use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;

$loop = \React\EventLoop\Factory::create();

$socketServer = new \React\Socket\Server('127.0.0.1:8080', $loop);

$httpServer = new \React\Http\Server(function(\Psr\Http\Message\ServerRequestInterface $request) {
  return new \React\Http\Response(200, [
      'Content-Type' => 'text/plain'
    ],
    'Hello, World'
  );
});

$httpServer->listen($socketServer);

$rrServer = new RRServer(); // Implements MessageComponentInterface

$webSocketServer = new IoServer(
  new HttpServer(
    new WsServer(
      $rrServer
    )
  ),
  $socketServer,
  $loop
);

$webSocketServer->run();

The code works, but I'm only able to access it using http://localhost:8080, and when I try to connect using WebSocket, the connection is opened, and then it immediately closes. Also, When I create a new socket with different port then I'm able to access both using http:// and ws://

Is it possible to run HTTP Server and WebSockets on the same port, and if yes, how to implement it?

Thanks.

@cboden
Copy link
Member

cboden commented Dec 28, 2019

You can not host two different applications on the same port. This is an operating system level restriction. I would recommend continuing to run your WebSocket server on 127.0.0.1:8080 which restricts connections on that server to connect then proxy all incoming port 80 connections through your web server, such as Nginx, and redirect traffic based on HTTP headers, route, or sub-domain.

@maliknaik16
Copy link
Author

@cboden There is no way we can run WebSocket and HTTP Server on same port using ReactPHP and Ratchet??

@cboden
Copy link
Member

cboden commented Dec 28, 2019

If they're both run by the same EventLoop that would be possible.

@maliknaik16
Copy link
Author

I've used the same EventLoop in my code but still it is not working.

@maliknaik16
Copy link
Author

Can you please provide me a solution for this?

Thanks in advance.

@cboden
Copy link
Member

cboden commented Dec 28, 2019

Your code is trying to attach two SocketServer's to the same port. You need to attach a single SocketServer to one port, then re-route traffic based on a condition of your choosing (HTTP header, endpoint, or sub-domain for example).

See Ratchet's App class or some of React's examples for some inspiration.

@WyriHaximus
Copy link

Take a look at: https://github.com/voryx/WebSocketMiddleware

@maliknaik16
Copy link
Author

maliknaik16 commented Dec 29, 2019

I'm unable to handle WebSocket and HTTP requests on one socket so I ended up creating two sockets running on different ports as the temporary solution.

<?php

use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;

$loop = \React\EventLoop\Factory::create();

$webSocketServer = new \React\Socket\Server('127.0.0.1:8080', $loop);
$httpSocketServer = new \React\Socket\Server('127.0.0.1:8081', $loop);

$httpServer = new \React\Http\Server(function(\Psr\Http\Message\ServerRequestInterface $request) {
  return new \React\Http\Response(200, [
      'Content-Type' => 'text/plain'
    ],
    'Hello, World'
  );
});

$httpServer->listen($httpSocketServer);

$rrServer = new RRServer(); // Implements MessageComponentInterface

$webSocketServer = new IoServer(
  new HttpServer(
    new WsServer(
      $rrServer
    )
  ),
  $webSocketServer,
  $loop
);

$loop->run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants