title: Node websocket client side listen to Golang websocket server
body: <p>I am trying to post messages using Golang websocket and listen on Node client side.</p>
  
  <p>Here's part of code:</p>
  
  <p>Golang:</p>
  
  <pre><code>func NewUpgrader() *websocket.Upgrader {
      return &amp;websocket.Upgrader{
          ReadBufferSize:  1024,
          WriteBufferSize: 1024,
          CheckOrigin: func(r *http.Request) bool {
              return true
          },
  }
  
  func (s WebsocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
      upgrader := NewUpgrader()
      ws, err := upgrader.Upgrade(w, r, nil)
      if err != nil {
          log.Errorf("websocket: could not upgrade %v", err)
          fmt.Fprintf(w, "websocket: could not upgrade %v", err)
          return
      }
  
      go func() {
          defer ws.Close()
          for msg := range s.Messages {
              json, _ := json.Marshal(msg)
              err := ws.WriteMessage(1, json)
              if err != nil {
                  log.Errorf("websocket: error sending message: %v", err)
              }
          }
      }()
  </code></pre>
  
  <p>By doing this I can get all the messages when I run the code below on Google console </p>
  
  <p><code>ws = new WebSocket('ws://localhost:8080/ws')
  ws.onmessage = function(msg) { console.log(JSON.parse(msg.data)) }</code></p>
  
  <p>And I add the same two lines code on my client side, now I got these errors</p>
  
  <p>Golang side:</p>
  
  <p><code>INFO[0009] 400 GET localhost:8080/ws (0 bytes) id: 77b7355412f2ffcd
  ERRO[0009] websocket: could not upgrade websocket: not a websocket handshake: 'upgrade' token not found in 'Connection' header</code></p>
  
  <p>Client side(browser console):</p>
  
  <p><code>GET http://localhost:8080/ws 400 (Bad Request)
  Failed to load http://localhost:8080/ws: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
  Uncaught (in promise) TypeError: Failed to fetch</code></p>
  
  <p>I tried to compare these two get request(Google console vs my client-side code). I found out that for google console there's a header <code>Connection:[Upgrade]</code>, but the header looks like this <code>Connection:[keep-alive]</code> in my client-side code.</p>
  
  <p>Not sure if this is the reason why I can not connect these two, any ideas?</p>
  
  <p>Thanks!</p>