webpack-dev-server icon indicating copy to clipboard operation
webpack-dev-server copied to clipboard

Can't proxy websocket request using proxy option

Open chalkbit opened this issue 11 years ago • 36 comments

The webpack-dev-server does not explicitly handle websockets through the proxy option. I would expect an entry like this to correctly proxy websocket requests:

proxy : [{path: /ws, target: ws://localhost:8080/, ws: true}]

After inspecting the Server.js file, it seem that there would need to be explicit handling of websockets. Something like this perhaps:

options.proxy.forEach(function (proxyOptions) {
    app.all(proxyOptions.path, function (req, res) {
        if(typeof proxyOptions.rewrite === 'function') proxyOptions.rewrite(req, proxyOptions);
        if (proxyOptions.host) {
            req.headers.host = proxyOptions.host;
        }
        if (proxyOptions.ws) {
            proxy.ws(req, res, proxyOptions, errorCb.bind(this));
        } else {
            proxy.web(req, res, proxyOptions, errorCb.bind(this));
        }
        function errorCb(err){
            var msg = "cannot proxy to " + proxyOptions.target + " (" + err.message + ")";
            this.io.sockets.emit("proxy-error", [msg]);
            res.statusCode = 502;
            res.end();
        }

        if (proxyOptions.configure) {
            proxyOptions.configure(proxy);
        }
    }.bind(this));
}.bind(this));

or add a new listener for websocket connections:

proxyServer.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

chalkbit avatar Sep 28 '15 18:09 chalkbit

@gymb321 I'd recommend using iframe mode (i.e. put a contentBase in your dev server config and go to localhost:port/webpack-dev-server/index.html in your browser). This is the only way I know of to connect to a backend server via websocket.

I'm still learning about web sockets but as far as I can tell there's no simple or standard way to proxy websocket traffic that's not intended for webpack-dev-server to the backend server.

jedwards1211 avatar Oct 07 '15 06:10 jedwards1211

+1

mingfang avatar Oct 19 '15 01:10 mingfang

:+1: Lack of websocket proxy support makes using dev-server with "real" backend servers that are based on ws impossible.

Jarema avatar Nov 01 '15 09:11 Jarema

:+1:

readme42 avatar Nov 18 '15 14:11 readme42

:+1:

andreasvoigt avatar Nov 18 '15 15:11 andreasvoigt

+1

geddski avatar Dec 02 '15 00:12 geddski

Looks to me that the issue with either of the suggested code snippets is that app.all(path, cb) in express doesn't fire for websocket connections, since express only listens for traditional HTTP(S) connections.

There are various middleware modules out there that try to solve this issue, such as express-ws, but I've had trouble getting them to work (possibly because the webpack-dev-server SockJS server is interfering?).

thomasboyt avatar Dec 03 '15 19:12 thomasboyt

Hacked together a quick solution, can turn into a PR if it seems like the right route: https://github.com/thomasboyt/webpack-dev-server/commit/f2fceae96952f4d24012c747924b55cbaba06f3c

thomasboyt avatar Dec 03 '15 20:12 thomasboyt

Hey @thomasboyt, thanks so much for that!!! I've been dying to be able to proxy websockets!

I had to make one change to get it to work; if you don't add an error handler to proxy, if it fires an 'error' event, the default error handler will throw the error, which seems to break something (I'm not sure what, the process just seems to hang, but since the error is probably getting thrown in the middle of stream communication it's probably not good). So if you simply add this underneath the requires:

proxy.on('error', function(err) {
    console.error(err.stack);
});

It fixes that issue. My sockets were sometimes hanging up so it was a major issue for me.

jedwards1211 avatar Dec 04 '15 20:12 jedwards1211

web sockets support will be very nice. I currently proxy them manually with a separate server.

geddski avatar Dec 04 '15 21:12 geddski

Thanks to @sokra and @SpaceK33z (and perhaps others?) web socket proxying (with socket.io) has been working for at least a couple of weeks now, since resolution #302. This is the config which is working (for me):

I have a socket.io server running on localhost:8001, on path /napi:

var io = require('socket.io')(http, { path: '/napi' })

In webpack config:

devServer: {
        proxy: {
            '/napi/*': {
                target: 'ws://localhost:8001',
                ws: true,
            },
        },
    },

In socket.io client:

var socket = io({ path: '/napi' })

I'm launching with: webpack-dev-server --inline --hot

ghost avatar Dec 05 '15 09:12 ghost

@apotropaix are you saying that websocket proxying should work without @thomasboyt's patch?

jedwards1211 avatar Dec 05 '15 16:12 jedwards1211

@jedwards1211 Yes. confirm. I have [email protected] installed.

ghost avatar Dec 05 '15 17:12 ghost

@apotropaix I see...I'm confused though, how is that possible when there aren't any proxy.ws() calls in that version's Server.js?

jedwards1211 avatar Dec 05 '15 17:12 jedwards1211

@apotropaix nevermind, I see how it works looking at node-http-proxy documentation. Thanks for enlightening us!

jedwards1211 avatar Dec 05 '15 17:12 jedwards1211

@apotropaix my project uses SockJS, and I can't get it to proxy successfully, whereas @thomasboyt's patch does work. Maybe the problem is that SockJS makes a mix of http:// and ws:// requests, but since the config can specify only one protocol in the target, one protocol will fail to get proxied?

jedwards1211 avatar Dec 05 '15 17:12 jedwards1211

@jedwards1211 Ah, I see, didn't catch you were using SockJS. There were similar issues with socket.io before issue 302 resolution switched the webpack-dev-server engine from socket.io to SockJS.

ghost avatar Dec 05 '15 19:12 ghost

@jedwards1211, what is the path your SockJS server lives at? webpack-dev-server uses /sockjs-node, so if you're also using this... I think we should move to using something like /_webpack-sockjs to prevent collisions like this.

SpaceK33z avatar Dec 05 '15 20:12 SpaceK33z

@SpaceK33z no, it lives at /sockjs, so there's no name collision. @apotropaix in any case, luckily Thomas Boyt's patch does work with SockJS...unless I misinterpreted something

jedwards1211 avatar Dec 05 '15 20:12 jedwards1211

Ah, this is interesting.

As far as I could tell, with my client using raw websockets, webpack-dev-server didn't seem to be even handling the proxied websocket connection. I suspect that either something about SockJS or socket.io's additions over regular websockets may allow them to work where regular websockets aren't (e.g. making HTTP connections that the express server is properly handling), or I'm just doing something entirely wrong in my proxy setup. I'll set up a test case and see what I can figure out :)

thomasboyt avatar Dec 07 '15 17:12 thomasboyt