ng-socket-io
ng-socket-io copied to clipboard
How do I connect to different namespaces?
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?
Hi! is this what you want : Multiple End points ?
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.
I understand now, it will be a good idea for a new feature, I will implement it as soon as possible, Thanks.
Hi,
Any updates on this feature on namespaces ?
thanks.
Hello, does this package support namespaces at all ?
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.
@pburrows what does your constructor
look like ?
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');
}
}
@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.
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.