Websockets server does not work live

I did a websocket server using Go here

But I do not think that this is a Go question, because this code works on my local computer.

But when moving to a VPS with Nginx proxy this fails. As this seems to “converts” from https:// to wss:// or ws:// I have no clue what I have done wrong. https://ws.go4webdev.org (live)

The console gives this error:

WebSocket connection to ‘wss://localhost:8080/echo’ failed:

Any clue how I can make this work?

I don’t know go but shouldn’t this

wss://localhost:8080/echo

be something like

wss://ws.go4webdev.org/echo

if you’re running it on the server instead of locally???

I have tried both with ws:// and wss://. Still no luck.

window.addEventListener("load", function(evt) {
  alert("value of ws at load " + ws)
  var output = document.getElementById("output");
  var input = document.getElementById("input");
  var ws;
  var print = function(message) {
    var d = document.createElement("div");
    d.textContent = message;
    output.appendChild(d);
    output.scroll(0, output.scrollHeight);
  };

  document.getElementById("open").onclick = function(evt) {
    alert("value at ws at open: " + ws)

    if (typeof ws !== 'undefined' && ws !== null) {
      return
    }
    
    alert("assign value to ws")
    ws = new WebSocket("ws://" + document.location.host + "/echo");
    alert("ws://" + document.location.host + "/echo") // this is not executed for some reason...
    alert(ws);

    ws.onopen = function(evt) {
      print("OPEN");
    }
    ws.onclose = function(evt) {
      print("CLOSE");
      ws = null;
    }
    ws.onmessage = function(evt) {
      print("RESPONSE: " + evt.data);
    }
    ws.onerror = function(evt) {
      print("ERROR: " + evt.data);
    }
    return false;
  };
  document.getElementById("send").onclick = function(evt) {
    if (!ws) {
      return false;
    }
    print("SEND: " + input.value);
    ws.send(input.value);
    return false;
  };
  document.getElementById("close").onclick = function(evt) {
    if (!ws) {
      return false;
    }
    ws.close();
    return false;
  };
});

Your alert should be telling you something. Where is ws defined other than a var in the load addEventListener? Seems like something is missing.

It is defined at load and assigned when clicking on button “open”

document.getElementById("open").onclick = function(evt) {
    // at load this should be undefined at this level AFAIK.
    if (typeof ws !== 'undefined' && ws !== null) {
      return
    }
    ws = new WebSocket("ws://" + document.location.host + "/echo");
    No matter if I use ws or wss here.

My assumption is that this is not fired for some reason:

ws = new WebSocket("ws://" + document.location.host + "/echo");

But it is fired at localhost and work properly. So the Go code at the server must be OK as I see it. The console reports no error.

More clues?

Stupid idea but did you try changing the ws: to http:?

I’m grasping at straws since I know nada about go…

And I do know nothing about websockets :-). From my limited understanding, you start a http server and “upgrade” to a ws server. Hence you cannot call the original http server to communicate with the websocket server. AFAIK.

My guts tell me that this is not about Go. It is about Websocket protocol and Javascript as client. Or any type of CORS issue involved.

I deleted the <form> element and try wss:// and got ONE step further by receiving an error message in the console

ws.js:21 WebSocket connection to ‘wss://ws.go4webdev.org/echo’ failed: Error during WebSocket handshake: Unexpected response code: 400

I think I found a solution. It seems that Websockets is an old protocol that is only supported by HTTP 1.1 (cannot confirm this). But when added this to the Nginx sites-available, it seems to work.

    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

If anybody can confirm this, I should be nice to know.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.