[help wanted] Manually proxy settings to open connection with websocket.org
See original GitHub issueHey 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:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
UPDATE: Final words.
If you would like to connect WebSocket server that already hosted at
ws://DOMAIN_URL
likews://echo.websocket.org
- you can’t use any client libraries like socket.io-client or ws. You need to use native WebSockets in the browserAlso, 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:@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):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:
Then, setup your proxy: