Skip to content

Can't proxy websocket request using proxy option #283

@chalkbit

Description

@chalkbit

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);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions