0

My websocket only works in php language, i need to create in javascript(nodejs), he dont works.

My PHP Code(Works perfectly):

server.php

<?php

require './vendor/autoload.php';

use Ratchet\Server\EchoServer;

$app = new Ratchet\App('localhost', 9980);
$app->route('/echo', new EchoServer, ['*']);
$app->run();

I created a websocket server on JavaScript:

var WebSocketServer = require('ws').Server;

var wss = new WebSocketServer({
    port: 8080
});

wss.on('connection', function(ws) {
    ws.on('message', function(message) {
         ws.send(message);
    });
});

When i send a message from Python script, doesnt work, only works with WebSocket PHP.

I just need to create a WebSocket in JavaScript(Node JS), PHP File to show messages from WebSocket, and the code to send messages to WebSocket(Python).

I think this is related to headers or something like this.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    "doesn't work" means what? Do you get a specific error? Also please provide a [mre] of the issue, including the code you use to make the connection. `I think this is related to headers`...because? Please explain your theory by providing some evidence. Speculation is usually not useful unless it has a clear basis. – ADyson Apr 30 '23 at 23:51
  • P.S. Why are you creating a websocket server using PHP _and_ also using nodeJS? You only need one of those, for a single application. – ADyson Apr 30 '23 at 23:52
  • "Doesn't work" = I dont receive websocket messages on WebPage, when i send on Python. I only receive messages when websocket server is php. – Lucas Silva Dev May 01 '23 at 00:06
  • "Why are you creating a websocket server using PHP and also using nodeJS?" = The PHP dont care, the server on nodejs is priority, i said php because i need to get messages on Web, is a WebPage(HTML). – Lucas Silva Dev May 01 '23 at 00:08
  • Whether you use PHP or nodeJS doesn't matter for that - both of them can provide websocket messages to a web page. Websockets don't care what kind of backend is running the server. – ADyson May 01 '23 at 00:10
  • `I dont receive websocket messages on WebPage, when i send on Python`...is your web page successfully connected to the nodeJS socket server, in that situation? Does the python code connect successfuly? We don't really know, as you haven't provided many details. And, again, we don't have a [mre] of the issue in terms of code either so it's very difficult to help you. Please [edit] your question with clearer information. Thanks. – ADyson May 01 '23 at 00:11
  • My WebPage and Python is connected, because, using PHP server its works. And, when its not connected, show error on Python Code and Error on DevTools(Web). This is my JavaScript code on my WebPage: `const socket = new WebSocket(); socket.addEventListener('message', function (event) { const data = JSON.parse(event.data); });` And my Python Code to send messages: `async with websockets.connect() as websocket: message_str = json.dumps(mensagem) await websocket.send(message_str)` – Lucas Silva Dev May 01 '23 at 00:20
  • My server PHP have a other file, a Class. I think need to reply this "function php on nodejs": `public function onMessage(ConnectionInterface $from, $msg): void { foreach ($this->clients as $client) { $client->send($msg); } }` – Lucas Silva Dev May 01 '23 at 00:22
  • If I add a log in nodejs, when the Python code sends a message, it appears in the console of the websocket(nodejs), the problem is that it doesn't send it to the web page, I added a console.log() in the web page to see any message new, and nothing appears. I think I need to add something in the js code to send the message to all connected clients. – Lucas Silva Dev May 01 '23 at 00:25
  • As I requested before, can you please [edit] your question to provide any new code and important information. It does not belong in the comments, which are supposed to be just for asking for brief clarification. See [ask] for more guidance, and the [tour] to understand more how stackoverflow works - it's not a discussion forum. Once the question info is coherent and all in one place it will be easier for people to understand it and to help you. Thanks – ADyson May 01 '23 at 00:34
  • Anyway yes you probably do need to add more in the JS code to broadcast the message. https://www.pubnub.com/blog/nodejs-websocket-programming-examples/ has examples, and I'm sure you can find lots more similar ones with a simple search on google – ADyson May 01 '23 at 00:36
  • It worked! Dude, I was about 10 hours into this, I'll pay for it if you want it, thanks a lot! – Lucas Silva Dev May 01 '23 at 00:52
  • Well that's kind but I didn't write the code, I literally just googled "nodejs websocket examples" and pasted a link which I thought looked useful. – ADyson May 01 '23 at 01:04
  • Thx, but a got other problem lol, the websocket cliente (webpage) only work on localhost, in host, dont connect :/ – Lucas Silva Dev May 01 '23 at 02:02
  • Do you get an error in the console when it fails to connect? Did you ensure the socket server is definitely running on the host? Are there any firewall rules you need to configure? These are a few things you need to think about. – ADyson May 01 '23 at 07:14

0 Answers0