angular-socket-io icon indicating copy to clipboard operation
angular-socket-io copied to clipboard

How do you connect to multiple namespaces with one connection?

Open shawn-simon opened this issue 9 years ago • 6 comments

Couldn't find an example. Thanks!

shawn-simon avatar Sep 01 '15 21:09 shawn-simon

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.

soorajvnair avatar Dec 04 '15 06:12 soorajvnair

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

shawn-simon avatar Dec 04 '15 06:12 shawn-simon

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

dolftax avatar Oct 21 '16 08:10 dolftax

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

afarazit avatar Jan 28 '17 17:01 afarazit

Is there a way of achieving this with 1 connection?

nikhilc94 avatar Jul 04 '18 10:07 nikhilc94

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

CILP avatar Jul 05 '18 21:07 CILP