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

Deferred Connection #37

Open
FreakTheMighty opened this issue Aug 28, 2014 · 3 comments
Open

Deferred Connection #37

FreakTheMighty opened this issue Aug 28, 2014 · 3 comments

Comments

@FreakTheMighty
Copy link

I am attempting to do token based authentication using socket.io and I'm interested in using this library.

The issue I'm running into is that I need to receive the token from the server before making the socket.io connection. The current design seems to require that I both instantiate and connect EmberSocket at the same time, and in my case, before I have the token to connect with.

What do you all think about separating the instantiation and the connection? Ideally I could do something like.

var App = Ember.Application.create({
  Socket: EmberSockets.extend({
      host: 'localhost',
      port: 9000,
      controllers: ['SomeController']
  }),
});

Then elsewhere in the authentication flow something like.

App.get('Socket').connect(token);
@vyobukhov
Copy link

I just added jquery.ajax call before $io.connect inside ember-sockets.js

Something like this

$jquery.ajax({
    type: 'POST',
    data: {data: JSON.stringify(some_data)},
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    url: loginServer,
    async: false
  }).done(function(result){ //Json answer with token
    token   = result.token;

    socket  = $io.connect(server, {
      secure: true,
      reconnection: false,
      'force new connection': true,
      'query': token ? 'token='+token : undefined
    });


    socket.on('error', function socketError(data){
      alert('Can\'t connect to socket.io server');
      throw 'Unable to make a connection to the Socket.io server >> ' + data;
    });

});

Works fine)) I don't know if it's good practice or not.

@Wildhoney
Copy link
Owner

I've moved the connection process into a connect method.

Defer the connection by setting autoConnect to false.

$window.App = Ember.Application.create({

    Socket: EmberSockets.extend({
        controllers: ['index'],
        autoConnect: false
    })

});

And then in your controller – such as your ApplicationController, manually connect to the WebSockets server using the connect method, passing in any params into the connect method:

this.socket.connect({
    query: { token: 123 }
});

Please let me know if this works for you guys! 👍

@FreakTheMighty
Copy link
Author

Awesome @Wildhoney. I haven't had a chance to try it yet, but that looks like it will work perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants