1

I'm running a simple websocket server in nodejs

const WebSocket = require('ws');

const wss = new WebSocket.Server({ host: '0.0.0.0', port: 13000 });

wss.on('connection', function connection(ws) {

        console.log("new connection");

        ws.on('message', function incoming(message) {
                      console.log('received: %s', message);
                    });

        ws.on('error', function(e) {
                console.log('error', e);
        });

        ws.on('close', function(e) {
                console.log('close', e);
        });

        ws.send('something');
});

I can connect to the server using:

s=new WebSocket('ws://localhost:13000')

and everything is fine. But if I try to connect from another computer:

s=new WebSocket('ws://serveripaddress:13000')

The server shows

new connection
close 1006

So I know that the client connects, but it then immediately disconnects. I am totally lost here, don't know how to diagnose my problem. Any ideas?

UPDATE

These scenarios work:

  • two ec2 instances: works
  • two linux VMs communicating over a host-only network: works

And these fail:

  • windows host machine and VM (host only network): fail
  • an ec2 instance and one of my VM's: fail

Here's the weird part, if I make a simple socket server using 'net', ALL scenarios above work! So it seems like its still a networking issue (since websockets work in some scenarios) but it is a networking issue specific to websockets (because socket server works where websocket doesnt)!

I dont have access to the firewall that my windows host machine is on so if that's the issue then I'm stuck. But I will try this all on a different host machine where I have access to the firewall.

But if it was the firewall, I would except it to work between the windows host and the VM because they are on their own virtual network...

chuck1
  • 665
  • 7
  • 22
  • What kind of client are you using? – jfriend00 Feb 22 '18 at 00:26
  • also nodejs, both are on linux – chuck1 Feb 22 '18 at 00:32
  • 1
    Personal Firewall on your server computer? Anti-virus? Does ping work reliably from client computer to server computer? – jfriend00 Feb 22 '18 at 00:34
  • If the server has a stateless firewall, make sure you open inbound for port 13000, and outbound 1024-65535. – programmerj Feb 22 '18 at 01:00
  • well i was running the server on an ec2 instance and the client on a VM. I'm not able to ping the server machine from the client, not sure why(followed instructions [here](https://stackoverflow.com/questions/21981796/cannot-ping-aws-ec2-instance)). I'm working on setting up another VM so i'll be communicating between VMs on the same host machine... i'll share my results – chuck1 Feb 22 '18 at 01:15
  • @chuck1 were you able to solve it? I am having same issue, but with heroku... – Do-do-new Aug 13 '19 at 13:06
  • 2
    @Empus Were you able to solve it? – amit gupta Mar 20 '20 at 17:12

0 Answers0