Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

389
Vistas
Node: How to establish a WS connection between two PCs of different networks?

My friend and I are trying to connect our PC's using sockets. So my friend runs a simple WS-server like this:

const WebSocket = require('ws')

function onConnect(wsClient) {

  wsClient.on('message', (message) => {
    const buff = Buffer.from(message)
    console.log('new message', buff.toString())
  })

  wsClient.on('close', function() {
    console.log('connection closed');
  })
}

wsServer.on('connection', onConnect);

Its purpose is to show a message in terminal once one is received.

I run a simple WS cliend like this one (I use my friend's public IP):

const WebSocket = require('ws')

wsServer = new WebSocket('ws://${IP}:9000')
wsServer.on('open', () => {
    wsServer.send(JSON.stringify('wsServer'))
})

When I run both a client and a server on my local machine everything works fine. When I try to reach my friend's PC I get en error like this:

Error: connect ETIMEDOUT ${IP}:9000

So I guess the problem is: when I hit the public IP the router does not know where to forward a request (since there are dozens of people who use it).

I have to general questions:

  1. Why there is no such error when I use some online chat? Its server can be a part of some local network as well.

  2. Is there a way to establish a safe P2P connection using sockets? If yes, how?

Thank you!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda