I've been looking for a detailed answer to this question for some time now, whether to start a new websocket on the server domain, or attach a receiver to the http server that is already running, for example Express with Node.js.
I have tried countless times in both directions: internal server client to internal-server host connection, or server to external client like the Javascript browser, but no success so far. Anyone have a working example or a link with a working example? I have tried countless times, even on the same server between two files as the example below.
The setup I have here from here, the server.js file
const WebSocketServer = require('ws');
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function message(data) {
console.log('received: %s', data);
});
ws.send('something');
});
Tnen there is the browser.js file
import WebSocket from 'ws';
const ws = new WebSocket('ws://127.0.0.1:8080/');
ws.on('open', function open() {
ws.send('something');
});
ws.on('message', function message(data) {
console.log('received: %s', data);
});
What the problem is, I really can't figure it out