0

I've never used before websockets neither web workers and everything around that. Obviously, I'm lost and I don't know how to make it properly.

I've learned how to make a websocket and leave it working in a server port successfully. As well, load a web worker (but this one can't refresh the website straight).

When a user is connected, everything is working fine. Websocket is sending messages to the user and the user is refreshing their website.

The problem is when I want to use it with many other users (different tabs or browsers to simulate different users). Only works with next user is connected with websocket, is not working anymore with the other users.

I share a diagram just to make it simple to understand what I want to do.

Another point is. Which language do I have to use to get this, both for the server and for the users? Python, NodeJs, Php (those are the only I know how to use).

enter image description here

Carlos Diaz
  • 321
  • 1
  • 15
  • It's unlikely anyone can help you without seeing the relevant code (probably sever-side if the problem is with multiple users). – jfriend00 Oct 16 '22 at 06:04
  • My code doesn't helps because as I told before, I'm totally lost. Just I need I guide about what I have to investigate and learn. – Carlos Diaz Oct 16 '22 at 06:11
  • The subject of your post makes it sound like you're trying to use a single webSocket connection for multiple separate clients. You can't do that. A webSocket connection is from ONE client to the server. – jfriend00 Oct 16 '22 at 06:14
  • Without your code, we really have no idea what you're doing wrong. There are thousands of examples on the web for how to make a webSocket connection from a client to a server. Use one of those that is relevant to your setup. Not sure what else to say when you won't show any code. – jfriend00 Oct 16 '22 at 06:14
  • I already did it, I took one of that examples but is only working with one client. Not even for two. – Carlos Diaz Oct 16 '22 at 06:19
  • You can use webSockets in any language you want. It is widely supported. If the client is a web page in a browser, then you'll certainly be using Javascript from the client and you can use pretty much any server-side language you want. Your question is tagged with nodejs and that would work just fine too. – jfriend00 Oct 16 '22 at 06:19
  • Then there's probably something wrong with your code. Can't help you with that problem without seeing THE code you are using. Questions about code here MUST include the relevant code. Otherwise, you're asking us to guess what is wrong. – jfriend00 Oct 16 '22 at 06:19
  • My websocket code is the same of this one https://javascript.info/websocket. Just, instead of simple message, I'm sending a random number. But when I try to access to the same websocket in the same port, the first tab stops working and just the new one is working. – Carlos Diaz Oct 16 '22 at 06:26
  • I guess I found a proper response in https://stackoverflow.com/questions/72081821/best-proper-way-to-share-a-single-websocket-connection-across-multiple-tabs. As you can see, to solve that question doesn't need to show any code. I'm going to check about share connections, shared webworkers and so on. – Carlos Diaz Oct 16 '22 at 06:40
  • Are you trying to have multiple clients sharing one webSocket connection, all in the same browser? Or are you trying to have multiple clients from different computers? The answer you refer to is only applicable to multiple tabs in the same browser. Which is your real world application problem? – jfriend00 Oct 16 '22 at 07:47
  • Ok, no. I need to open a websocket server connection under server port and after, the mainpoint is, to get any user account, with any device connected to same websocket. Exactly as the picture show. – Carlos Diaz Oct 17 '22 at 11:07
  • Multiple separate devices cannot use the same webSocket. They would each make their own webSocket connection to the server. – jfriend00 Oct 17 '22 at 21:41
  • Solve it! multiple separate devices can connect with same websocket. Just I generate a number assigned to each client and I send the random number generated by server to each connection! – Carlos Diaz Oct 18 '22 at 11:21

1 Answers1

0

SOLVED!

Just I generate a number assigned to each client (can be different device between each other) and I send the random number generated by server to each connection!

Before "connection" you shoul add:

const WS = require('ws');
const WS_PORT = 8081
const express = require('express');
const app = express();
const PORT = 3000;

app.listen(PORT, () => console.log(`Server listening , go to http://localhost:${PORT}`));
app.use(express.static('public'));


const wss = new WS.Server({ port: WS_PORT })

const wsSelected = new Set();
// Creating connection using websocket

const interval = setInterval(() => {
    const randomNumber = Math.floor(Math.random() * 100);
    //Sending same number to each client
    wsSelected.forEach(ws => 
        ws.send(randomNumber)
    )
}, 2000);

After "connection" you should add:

wss.on("connection", ws => {
    console.log("New client!");
    
    //This line you should add
    wsSelected.add(ws);

    ...
Carlos Diaz
  • 321
  • 1
  • 15