bougarfaoui / ng-socket-io

Socket.IO module for Angular
MIT License
254 stars 57 forks source link

How do I connect to different namespaces? #32

Open pburrows opened 6 years ago

pburrows commented 6 years ago

If I set the config in an @NgModule to be to a given namespace, all modules wind up trying to use that same namespace (last one registered seems to win).

I would like to have different Services able to connect to different namespaces. Or even a single service that connects to a bunch of different namespaces (though that would be rare).

How can I do that?

bougarfaoui commented 6 years ago

Hi! is this what you want : Multiple End points ?

pburrows commented 6 years ago

No, not really. I don't think that would work the same way (unless internally socket.io reuses the connection). The namespaces for socketio work more like this:

class 1:

constructor() {
        super({ url: 'http://url_one:portOne/namespace1', options: {} });
 }

class 2:

constructor() {
        super({ url: 'http://url_one:portOne/namespace2', options: {} });
 }

So the /namespace1 /namespace2 is the only thing that changes at the end of the path. Internally, socketio uses the same socket connection to pass information for both namespaces.

Your example looks like it would be more for different servers. Though, it might just coincidentally work the same.

bougarfaoui commented 6 years ago

I understand now, it will be a good idea for a new feature, I will implement it as soon as possible, Thanks.

darrelmen commented 6 years ago

Hi,

Any updates on this feature on namespaces ?

thanks.

radiumrasheed commented 6 years ago

Hello, does this package support namespaces at all ?

pburrows commented 6 years ago

FWIW, I wound up just making my own copy of https://github.com/bougarfaoui/ng-socket-io/blob/master/socket-io.service.ts and adding support for specifying the namespace in the constructor. That's really all you need from this library, anyway.

radiumrasheed commented 6 years ago

@pburrows what does your constructor look like ?

pburrows commented 6 years ago
constructor(namespace?: string) {
        let url: string = this.apiBase;
        if (namespace) {
            url += namespace;
        }
        const opts: SocketIOClient.ConnectOpts = <SocketIOClient.ConnectOpts>{};
        this.ioSocket = io(url, opts);
    }

Simply concatenates a passed in namespace if there is one. Consuming it then looks something like:

export class MyService extends SocketIoBaseService {
    constructor() {
        super('/my-namespace');
    }
}
Simbaclaws commented 5 years ago

@pburrows Could you perhaps make a pull request with these changes so that they can be merged into this repository? I would also like to be able to connect to a namespace. If you don't want to do this, I'll fork this repository and make the pull request myself. But since you're the one who figured it out, I suppose it's your glory for the credits.

Keeping a copy of the original source code locally is not ideal, nor is creating a separate fork repository.

@radiumrasheed Could you add this code to the project so people can connect to namespaces?

I'm rather surprised this isn't already in there since it's been a year with a working code example...

EDIT: apparently there is a newer fork of this repository called ngx-socket-io that does have namespace support. Sorry for bumping old issues.

pburrows commented 5 years ago

Hey @Simbaclaws , I don't have that project current anymore. It's been awhile since I did this. :-) If you have it implemented already, go ahead and make a PR. If not, I'll try and get back to it.