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: true
to 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.js
and added acomponentDidMount
that creates a websocket. It tries to connect to localhost:3000, the webpack dev server.Then, in
packages/react-scripts/package.json
, added theproxy
option:(it works with
http
and withws
-- no need forws
though because of thews: true
flag)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 start
The 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-client
Start up the server and CRA, and the server terminal should print "hey now!"