Enable proxying of websockets#1090
Merged
gaearon merged 1 commit intofacebook:masterfrom Nov 23, 2016
Merged
Conversation
Added `ws: true` to the httpProxyMiddleware options, and also listen for the "upgrade" event so that websockets can be proxied immediately, rather than waiting for an initial HTTP request.
Contributor
|
LGTM. Thanks! |
jarlef
pushed a commit
to jarlef/create-react-app
that referenced
this pull request
Nov 28, 2016
Added `ws: true` to the httpProxyMiddleware options, and also listen for the "upgrade" event so that websockets can be proxied immediately, rather than waiting for an initial HTTP request.
alexdriaguine
pushed a commit
to alexdriaguine/create-react-app
that referenced
this pull request
Jan 23, 2017
Added `ws: true` to the httpProxyMiddleware options, and also listen for the "upgrade" event so that websockets can be proxied immediately, rather than waiting for an initial HTTP request.
randycoulman
pushed a commit
to CodingZeal/create-react-app
that referenced
this pull request
May 8, 2017
Added `ws: true` to the httpProxyMiddleware options, and also listen for the "upgrade" event so that websockets can be proxied immediately, rather than waiting for an initial HTTP request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added
ws: trueto the httpProxyMiddleware options, and also listenfor the "upgrade" event so that websockets can be proxied immediately,
rather than waiting for an initial HTTP request.
How To Test This
I modified the template
App.jsand added acomponentDidMountthat creates a websocket. It tries to connect to localhost:3000, the webpack dev server.Then, in
packages/react-scripts/package.json, added theproxyoption:(it works with
httpand withws-- no need forwsthough because of thews: trueflag)Finally, I set up a tiny Express echo server:
app.js
Package-wise it just needs
npm install express express-ws.In one terminal, start up the WS server:
node app.js.In another terminal, start up CRA:
npm startThe browser console should show that the socket connected, and say "hey there!" The WS server terminal should also say "hey there!"
Still Works With Socket.io
I tested with socket.io as well -- that still works.
Express server: (
npm install express socket.io)Same proxy settings.
In
App.js, import socket.io:import io from 'socket.io-client';Connect and send a message:
Install the client:
npm install socket.io-clientStart up the server and CRA, and the server terminal should print "hey now!"