Can't make web socket connection as client

  • I cannot connect to a web socket server. Using exactly the same client code on a PC works fine. I've tried running the server both in the cloud and on my LAN, but the results are the same. I've simplified the code as far as possible:

    const WebSocket = require('ws');
    const sockets = () => {
      const ws = new WebSocket('ws://xxxxxxxxx:9090')
      ws.on('open', () => console.log('Connection opened'));
      ws.on('message', data => console.log(data));
      ws.on('close', () => console.log('Connection closed'));
      ws.on('error', error => console.log('ERROR:', error));
    // TODO: Check connection exists first, rather than waiting
    setTimeout(() => sockets(), 20000);

    Output is as follows:

    --- User program's output: ---
    [09:37:57] Connecting...
    [09:37:58] ERROR: { Error: Connection reset by peer (at read)
    [09:37:58] at [anon] internal code: 'ECONNRESET', errno: -104, syscall: 'read' }                                             
    [09:37:58] Connection closed

    The websocket server does not throw any errors and shows no indication that it has seen the client.

    I have run tcpdump on the server to confirm the client is indeed actually reaching the server hardware. It is, as confirmed by the following:

    sudo tcpdump -ni any port 9090
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
    09:47:55.281171 IP [Client IP].3164 > [Server IP].9090: Flags [S], seq 10543, win 5744, options [mss 1440], length 0
    09:47:55.281235 IP [Server IP].9090 > [Client IP].3164: Flags [S.], seq 526312017, ack 10544, win 29200, options [mss 1460], length 0
    09:47:55.301062 IP [Client IP].3164 > [Server IP].9090: Flags [.], ack 1, win 5744, length 0
    09:47:55.340027 IP [Client IP].3164 > [Server IP].9090: Flags [P.], seq 1:232, ack 1, win 5744, length 231
    09:47:55.340087 IP [Server IP].9090 > [Client IP].3164: Flags [.], ack 232, win 30016, length 0
    09:47:55.340699 IP [Server IP].9090 > [Client IP].3164: Flags [F.], seq 1, ack 232, win 30016, length 0
    09:47:55.365059 IP [Client IP].3164 > [Server IP].9090: Flags [.], ack 2, win 5743, length 0

    Any help with this will be very much appreaciated 🙂

  • Hello!
    I did some tests..

    Can you please try to change the URL in new WebSocket(...) from ws://xxxxxxxxx:9090 to ws://xxxxxxxxx:9090/
    Said differently: Please try to add the slash.

    If that fixes it, I know what to change in low.js so it behaves like Node.JS and so WebSocket works without slash.
    Thank you,

  • Thank you so much Thomas - that fixed it! I can't believe it was such a minor detail. I've been bashing away at this all morning 😁
    Really enjoying lowjs BTW!

  • Good to hear! WIll make it work without the slash in the next update.

  • Now works without / in the current update!

  • Awesome - thanks Thomas 🙂
    BTW, what is the ^ 0 v bottom right of each post? I clicked it before because I thought it was like a +1 thumbs, but now I'm not sure!

  • I think that is what is supposed to be.

Log in to reply