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

How can I host ratchet into the internet? #1074

Open
amiel0606 opened this issue Oct 20, 2024 · 6 comments
Open

How can I host ratchet into the internet? #1074

amiel0606 opened this issue Oct 20, 2024 · 6 comments

Comments

@amiel0606
Copy link

Sorry if this question is already asked but I am stuck with this problem for 2 days now, I need to host my website into a domain, it is all working but the websocket connect is not established. I have purchased a VPS already but it is not still working and I do not know what to do

@issam0999
Copy link

hello,
did you solve the issue? i have the same issue and i need help

@amiel0606
Copy link
Author

hello, did you solve the issue? i have the same issue and i need help

Hi, I have resolved it but it only works in http and not in SSL. I wasn't able to proxy it using SSL.

@ev-gor
Copy link

ev-gor commented Jan 8, 2025

@amiel0606 First, tell what proxy server you use and show your proxy and ratchet config

@issam0999
Copy link

clients = new \SplObjectStorage(); $this->sessionClients = []; } public function onOpen(ConnectionInterface $conn) { $queryParams = []; parse_str($conn->httpRequest->getUri()->getQuery(), $queryParams); $sessionId = $queryParams['session_id'] ?? null; if ($sessionId) { $this->clients->attach($conn); $this->sessionClients[$sessionId] = $conn; echo "New connection (Session ID: $sessionId, Connection ID: {$conn->resourceId})\n"; } else { echo "Connection rejected: No session ID provided.\n"; $conn->close(); } } public function onMessage(ConnectionInterface $from, $msg) { echo "Received message: $msg\n"; $data = json_decode($msg, true); if (json_last_error() !== JSON_ERROR_NONE) { echo "Invalid JSON received.\n"; return; } if (!isset($data['channel'])) { echo "No channel specified in the message.\n"; return; } if (isset($data['users'])) { $users = $data['users']; foreach ($users as $userId) { if (isset($this->sessionClients[$userId])) { if ($userId != $data['senderUserId']) { $this->sessionClients[$userId]->send($msg); echo "Message sent to Session ID: $userId\n"; } } else { echo "Session ID: $userId is not connected.\n"; } } } else { foreach ($this->clients as $client) { $client->send($msg); } } } public function onClose(ConnectionInterface $conn) { foreach ($this->sessionClients as $sessionId => $clientConn) { if ($clientConn === $conn) { unset($this->sessionClients[$sessionId]); echo "Connection closed (Session ID: $sessionId, Connection ID: {$conn->resourceId})\n"; break; } } $this->clients->detach($conn); } public function onError(ConnectionInterface $conn, \Exception $e) { echo "An error occurred: {$e->getMessage()}\n"; $conn->close(); } } // Create an event loop $loop = EventLoopFactory::create(); echo "Starting WebSocket server with SSL...\n"; // Create a socket server $socketServer = new SocketServer('0.0.0.0:8081', [], $loop); // Wrap it with SSL try { $secureSocket = new SecureServer($socketServer, $loop, [ 'local_cert' => 'certificates/fullchain.pem', 'local_pk' => certificates/privkey.pem', 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER | STREAM_CRYPTO_METHOD_TLSv1_3_SERVER, 'verify_peer' => false, ]); } catch (\Exception $e) { echo "SecureServer error: " . $e->getMessage() . "\n"; exit(1); } // Start the WebSocket server $server = new IoServer( new HttpServer( new WsServer( new NotificationServer() ) ), $secureSocket, $loop ); echo "WebSocket server started\n"; $loop->run();

@issam0999
Copy link

I am using apache with php on cpanel ubuntu

@ev-gor
Copy link

ev-gor commented Jan 10, 2025

OK, here is a config for a secure websocket connection with Apache:

    #we must add special headers to the websocket request
    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://127.0.0.1:8080/$1 [P,L]

    #redirect an external request to our local websocket server
    ProxyPass /websocket  ws://localhost:8080
    ProxyPassReverse /websocket ws://localhost:8080

This code should be added to your Apache's ssl conf file. By default, it's located at /etc/apache2/sites-available/default-ssl.conf
Don't forget to enable Apache's proxy and proxy_http modules, as well as modules required for ssl.
Replace port 8080 with your actual port. If your local websocket server uses an encrypted connection, replace ws protocol in my code to wss. '/websocket' in ProxyPass and ProxyPassReverse is just an arbitrary path that clients will use. You can change it (both in Apache and client config) to whatever you like.
To check that everything is working, first ensure that your websocket server is operational by making a local websocket request. Then, verify that Apache redirects external requests. Use some online websocket client to make a request to 'wss://<your_domain>/websocket'. The connection should be established with status code 101. This is a wss (i.e. secured, encrypted) connection.

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

No branches or pull requests

3 participants