btford / angular-socket-io

Socket.IO component for AngularJS
1.51k stars 233 forks source link

How do you connect to multiple namespaces with one connection? #119

Open shawn-simon opened 9 years ago

shawn-simon commented 9 years ago

Couldn't find an example. Thanks!

soorajvnair commented 8 years ago

Hi Shawn. It's a straightforward thing. You can create separate factories for each namespace, and initialize respective connections inside of it. Here's an example.

suppose you have two namespaces. /feed and /chat

for /feed namespace

app.factory('feedSocket',feedSocket);

function feedSocket(socketFactory){

     var myIoSocket = io.connect('server-base-url' +/feed);
     var mySocket = socketFactory({ ioSocket : myIoSocket });
     return mySocket;

}

for /chat namespace

app.factory('chatSocket','chatSocket');

function chatSocket(socketFactory){

     var myIoSocket = io.connect('server-base-url' + '/chat');
     var mySocket = socketFactory({ ioSocket : myIoSocket });
     return mySocket;

}

then inside your controllers you can simple inject the required socket factory. Hope it's clear for you.

shawn-simon commented 8 years ago

Thanks, this is really helpful. I did it a different way but will probably switch it to this

dolftax commented 8 years ago

Does this make two separate connections? If so, why?

afarazit commented 7 years ago

@dolftax Yes it does because it is called from different factories with different namespaces

nikhilc94 commented 6 years ago

Is there a way of achieving this with 1 connection?

CILP commented 6 years ago

@nikhilc94 https://github.com/socketio/socket.io-client-swift/issues/837