Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket support #1

Closed
aaronchar opened this issue Apr 3, 2015 · 10 comments
Closed

WebSocket support #1

aaronchar opened this issue Apr 3, 2015 · 10 comments

Comments

@aaronchar
Copy link

Just wondering if this will work with web sockets as well.

@chimurai
Copy link
Owner

chimurai commented Apr 4, 2015

I think it'll just take little effort to add socket support, as http-proxy already has support to proxy sockets.

Do you have an example how you would like to use it?

@aaronchar
Copy link
Author

Well it would be nice to have similar functionality to grunt-connect.

I believe the underlying library you are using supports websockets.

But other then that this library is perfect thanks for the work you have
put into it.
On Apr 4, 2015 9:23 AM, "chimurai" notifications@github.com wrote:

I think it'll just take little effort to add socket support, as http-proxy
already has support to proxy sockets.

Do you have an example how you would like to use it?


Reply to this email directly or view it on GitHub
#1 (comment)
.

@chimurai
Copy link
Owner

chimurai commented Apr 6, 2015

Websockets are still unfamiliar terrain for me...

Would it be as simple as:
var proxy = proxyMiddleware(context, {target: 'ws://echo.websocket.org'});

So all websocket requests will be proxied to "ws://echo.websocket.org", when using this as middleware?

Btw: feel free to create pull requests for any missing feature, or create separate issues for them.

@aaronchar
Copy link
Author

Well I am by no means all that knowledge about sockets either.

But from what I understand we would need to wait for an upgrade event then switch over to use proxy.ws from proxy.web, That being said I have tried it a few different ways and always end up with the socket immediately closing.

@aaronchar
Copy link
Author

grunt-connect-proxy

That seems to use http-proxy and works for proxying websockets

@chimurai
Copy link
Owner

chimurai commented Apr 7, 2015

Could you provide a gist of the setup you tried?
Than I'll take a shot at it.

@aaronchar
Copy link
Author

Sure, I moved to use trying to use http-proxy without this library to see if I could get it working but ended up with the same result, I am sure I have missed something.

I don't have my code with me but here is a quick setup idea, Hopefully it gives you an idea of what I was trying to do.
Example

@chimurai
Copy link
Owner

chimurai commented Apr 8, 2015

I got a semi-working WebSocket setup with "connect":

gulp.task('serve:ws-connect', function () {
    var proxy = proxyMiddleware('/', {target: 'http://www.websocket.org', proxyHost:true});

    var app = connect();
    app.use('/', proxy);

    var server = http.createServer(app).listen(3000);
});

Initiate a WS connection in the browser:

var socket = new WebSocket('ws://localhost:3000');

With the above code I'm able to

  • proxy the http website of www.websocket.org
  • proxy the websocket demo on their site.

Some pitfalls:
At least 1 http request had to be made in order to access connect's server object, so I can subscribe to the server.on('upgrade') event and proxy the subsequent WebSocket call. (As indicated in your previous example req.connection.server)


Tried browser-sync too:
Couldn't get it working at all.
Not sure why it is happening.
I think the build-in socket server is occupying the same port, even when I change browsersync's socket.namespace property.


As middleware, I haven't found an elegant way to access the server object to listen to the upgrade event, (without making a normal initial http request)


If you want to play with the code, just replace the following function in the middleware:

    function fnProxyMiddleWare (req, res, next) {
        if (utils.hasContext(context, req.url)) {

            proxy.web(req, res);

            if (!server) {
                server = req.connection.server;
                req.connection.server.on('upgrade', function (req, socket, head) {
                    console.log('[HPM] Upgrading to WebSocket');
                    req.headers.host = 'echo.websocket.org';          // should make this dynamic.
                    proxy.ws(req, socket, head);
                });
            }

        } else {
           next();
        }
    }

@aaronchar
Copy link
Author

Sorry for the late response.
I am going to try and spend some time this week playing with it, But thank you for taking a look at it. I am just making the switch from Grunt => Gulp and sadly this has been the only sticking point.

@chimurai
Copy link
Owner

chimurai commented Apr 9, 2015

No worries.
I was actually switching from Grunt => gulp as well.
In the process I bumped into some corporate network issues, which I didn't experience with the grunt-contrib-connect + grunt-connect-proxy setup.

Ended up writing this tiny middleware... :)

grunt-connect-proxy uses the same websocket upgrade strategy:
//Listen for the update event,onces. grunt-contrib-connect doesnt expose the server object, so bind after the first req
https://github.com/drewzboto/grunt-connect-proxy/blob/master/lib/utils.js#L113

Let me know what your findings are.

@chimurai chimurai changed the title Sockets WebSocket support Jul 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants