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

Add support for deferred sockets. #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mritzco
Copy link

@mritzco mritzco commented Feb 10, 2015

Add support for deferred sockets. Sockets that require user or application interaction before connecting, it also allows for socket swapping.

It's based on:
#86
Provides an alternative way to:
#64

I added a README-deferred.md to explain in more detail and keep the js file clean.

It currently passes all the tests, added tests with socket replacement before calls and after calls.
These changes are based on the work of @davisford but refactored to:

  • Preserve original module (by @btford) functionality and operation nearly intact
  • Allow swap sockets (between real io-sockets)
  • Follow the same order and structure as original @btford module for easier maintenance
  • Acts as an endpoint insted of modifying or rewrapping angular-socket-io
  • Pass all tests

Needs tests for second real socket swap (real socket for another real socket)

…cation interaction before connecting)

Allow socket swapping
@davisford
Copy link

Nice work!

@mritzco
Copy link
Author

mritzco commented Feb 11, 2015

Glad you liked it and thanks for your code it was a great guide.
On Feb 11, 2015 9:40 PM, "Davis Ford" [email protected] wrote:

Nice work!


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

@coommark
Copy link

Awesome. This is exactly what I am looking for. In my opinion its actually more common to connect to realtime backends after some activity(in production), hardly ever on app initialization. So thanks for the awesome job. So if I download mritzco/angular-socket-io I get this instead of the original btford's? I am kind of still confused about github. I checked mritzco/angular-socket-io didn't find the README-deffered.md. Where do I download everything?

  1. Any plans to add disconnected events?
  2. What's the hypothetical use case of swapping sockets? I love the idea just want to know a sample use case. Is it like "for unauthenticated users, I show them these update, when they authenticate, I swap sockets and show them something else"?

BTW thank you @davisford! I started using your gist, my problem is I couldn't figure our where to add socket.on. (Is it in the controller or factory? Sorry I am from .net and signalr. Only been on node and angualr for two months. The socket.io thing is still very confusing to me and all the books don't use angular in their samples.)

@coommark
Copy link

Sorry I got and downloaded it! Thanks!

@coommark
Copy link

Hi! Now I really need help. Been trying this for hours without success. My code is like this:

app.factory('realtime, ['$rootScope', 'socketFactory', 'defferedSocketFactory', function(etc) {
deferred_socket = deferredSocketFactory();
socket = socketFactory({
ioSocket: deferred_socket
});
$rootScope.$on('authenticated', function() {
//I fetch auth
var realSocket = io.connect(serviceBase, { query: 'Bearer=' + auth });
socket.swapSocket(realSocket);
});
//Here is the problem
socket.forward('update');
return deferred_socket;
}]);

I access this from another factory
$rootScope.$on('socket:update, function(event, data) {
alert('received');
});

The connection is being made to the server okay. But I am not able to receive. When I was using btford's, I could receive it, but not I can't. Pls what am I doing wrong?

@mritzco
Copy link
Author

mritzco commented Apr 22, 2015

Hi,

Questions

  1. Any plans to add disconnected events?
    If we're disconnected what would generate the event? Do you have an example of the concept/cases that would cover?
  2. What's the hypothetical use case of swapping sockets? I love the idea just want to know a sample use case. Is it like "for unauthenticated users, I show them these update, when they authenticate, I swap sockets and show them something else"?

It really depends on the nature of your application, for us it makes possible to swap sockets from web to local intranet for locations with bad/no internet access.

How to install:
You can do it with bower, for the modified branch you can use:
In bower.json
"angular-socket-io": "https://github.com/mritzco/angular-socket-io/tarball/deferred",

More info here:
http://metalmatze.de/blog/installing-a-specific-commit-branch-or-version-from-a-git-repository-with-bower

Problem
Object creation and swapping code seems correct, I think the error might arise from the code sequencing.
a) Why are you waiting for an event before returning the deferred_socket? As the code is written now you just return the socket and use it as if it were connected. ( Your app of course should reflect the differences in state/functionality between connected/disconnected modes )
b) I would also check when the update event is triggered to see the event actually happens when you are expecting it.

Normally you would work with the socket like this:

Factory: Return the socket immediately so it can be injected anywhere else by angular:
From:

socket = socketFactory({

either return socket or return it immediately

return socketFactory({ ...
//OR keep code as before and:
return socket;

From that point you can just refer to socket as is it were connected and request angular to inject it.

angular.module('myapp').controller|factory|service(['ngSocket', function (socket) {
   socket.on('test',...);
}]);

The code for swapping can remain in the factory or anywhere outside (socket is injected as in previous paragraph)

$rootScope.$on('authenticated',

Finally:
socket.forward, this event should be properly passed to the new swapped socket right after swap. You can check if there are events in queue (console.log in the deferred socket), and please double check the event is triggered when you expect it, but if the purpose of that function is to make the socket available to other controllers/factories then you don't really need to do it. Just use the injected socket as if it were a normal btford one.

Let me know if that helps, good luck!

@sunild
Copy link

sunild commented Feb 18, 2016

If anyone else comes here looking for this functionality, I was able to achieve something similar using the existing functionality in the repo. Instead of swapping the sockets, just set the autoConnect option for Socket.io to `false:

function socketConnection(socketFactory) {
  return socketFactory({
    prefix: '',
    ioSocket: io.connect('http://localhost:8083', {
      autoConnect: false
    })
  })
}

Now you can inject the above socketConnection where you need it, and call it's connect() method when your application logic deems it necessary.

@pciazynski pciazynski mentioned this pull request Apr 7, 2016
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

Successfully merging this pull request may close these issues.

4 participants