question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[help wanted] Manually proxy settings to open connection with websocket.org

See original GitHub issue

Hey hi.

Since react-scripts@1.0.0 and higher create-react-app support manual proxy settings.

After creating app like

creare-react-app proxy-test

I installed socket yarn add socket.io-client, added proxy settings to the package.json like:

  "proxy": {
    "/": {
      "target": "ws://echo.websocket.org",
      "ws": true
    }
  }

changed src/App.js:

import io from 'socket.io-client';

class App extends Component {
  constructor(props) {
    super(props);
   
    this.socket = io();
  }

  componentDidMount() {
    console.log('MOUNTED');
    this.socket.on('connect', () => console.log('Connected to the socket');
  }

But when I’m opening the console, I can see that request actually goes through HTTP and I’m getting 404 response, because there is no that page (this page exists only for web socket request)

Request URL: http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LmXuie9
Request Method: GET
Status Code: 404 Not Found

Expected behavior:

Request URL: wss://localhost:3000/socket.io/?EIO=3&transport=polling&t=LmXuie9
Request Method: GET
Status Code: 101 Web Socket Protocol Handshake

When I’ll resolve that, will open PR with an example in improved documentation.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
yuzhvacommented, May 21, 2017

UPDATE: Final words.

If you would like to connect WebSocket server that already hosted at ws://DOMAIN_URL like ws://echo.websocket.org - you can’t use any client libraries like socket.io-client or ws. You need to use native WebSockets in the browser

Also, you don’t need to configure a proxy for that purpose.

The solution will be: After creating app like creare-react-app proxy-test, connect to WebSocket server in src/App.js:

class App extends Component {
  constructor(props) {
    super(props);
   
    this.socket = new WebSocket('ws://echo.websocket.org');
  }

  componentDidMount() {
    this.socket.onmessage = (message) => {
      const messageData = JSON.stringify(message);

      console.log(messageData);
    }
    
    // Give some time for socket to establish connection
    setTimeout(() => {
      this.socket.send('create-react-app');
    }, 3000);
  }
1reaction
jamesblightcommented, May 21, 2017

@YUzhva @gaearon I spent a bit of time investigating this issue. The proxy is working, but ws://echo.websocket.org is not running a socket.io server. This is why you’re seeing a 404 response. If you use socket.io, you need to use a socket.io server.

If you look at the network requests with and without the proxy defined in package.json, you can see that the behaviour is slightly different. With proxy, there’s a 404. Without, there’s a 200 GET and then a 404 POST. You’ll also see different behaviour if you proxy to the secure websocket (You get a 500 error):

 "proxy": {
    "/": {
      "target": "wss://echo.websocket.org",
      "ws": true,
      "secure": true
    }
  }

TLDR; socket.io client will only work with a socket.io server, and websocket.org doesn’t support this. https://github.com/socketio/socket.io/issues/533

If you want to proxy a websocket connection, you can use plain Websockets (without socket.io) https://github.com/websockets/ws#echowebsocketorg-demo

If you want to use socket.io, you can run your own local socket.io server to connect to:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(3001);

function handler (req, res) {
  res.writeHead(200);
  res.end('');
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('other', function (data) {
    console.log(data);
  });
});

Then, setup your proxy:

"proxy": {
  "/socket.io": {
    "target": "ws://localhost:3001"
    "ws": true
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How HTML5 Web Sockets Interact With Proxy Servers - InfoQ
Peter Lubbers explains in this article how HTML5 Web Sockets interact with proxy servers, and what proxy configuration or updates are needed ......
Read more >
Writing WebSocket servers - Web APIs | MDN
First, the server must listen for incoming socket connections using a standard TCP socket. Depending on your platform, this may be handled for ......
Read more >
426736 - WebSocket connections not using configured system ...
Manually configured HTTP and HTTPS proxies are ignored for WebSockets. Confirmed on Linux KDE, but I expect other platforms manual configuration is also...
Read more >
WebSocket - The Modern JavaScript Tutorial
To open a websocket connection, we need to create new WebSocket using the ... (using headers) asks the server: “Do you support Websocket?...
Read more >
Fiddler not showing websocket protocol using ws prefix - Telerik
When the client is configured to use an HTTP proxy and it wants to establish a WebSocket connection with a server, firstly it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found