Skip to content

Deferred Connection #37

Open
Open

Description

@FreakTheMighty

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

Activity

  1. vyobukhov commented on Aug 28, 2014

    @vyobukhov

    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.

  2. Wildhoney commented on Aug 30, 2014

    @Wildhoney
    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! 👍

  3. FreakTheMighty commented on Sep 2, 2014

    @FreakTheMighty
    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

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