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

How to use .of(namespace)?

Open joschne opened this issue 6 years ago • 17 comments

Hi! i was trying to use .of(namespace) but I could not figure out how to use it.

When I injected Socket in my Angular Service and called .of('myNamespace') on the WrappedSocket instance I got the error:

ERROR TypeError: this.ioSocket.of is not a function at WrappedSocket.push../node_modules/ngx-socket-io/src/socket-io.service.js.WrappedSocket.of (socket-io.service.js:21)

It would be great to have a little documentation / an example about how to use this function.

Thank you!

joschne avatar Jan 09 '19 13:01 joschne

I also couldn't figure out how to use it.

However, e.g., if chat is your namespace, then you need to supply it in the url of configuration like this:

const config: SocketIoConfig = {url: 'http://localhost:8080/chat', options: {}};

Be aware that this would restrict you to chat namespace globally for each socket. To avoid that, I would suggest using something like this:

import { Injectable, NgModule } from '@angular/core';
import { Socket } from 'ngx-socket-io';

@Injectable()
export class ChatSocket extends Socket {
  constructor() {
    super({url: 'http://localhost:8080/chat', options: {}});
  }
}

@NgModule({
  declarations: [
    //components
  ],
  imports: [
    SocketIoModule,
    //...
  ],
  providers: [ChatSocket],
  bootstrap: [/** AppComponent **/]
})
export class AppModule { }

etherealyn avatar Feb 20 '19 18:02 etherealyn

I have the same error when trying to use .of(). Any updates on this?

lincond avatar Mar 13 '19 16:03 lincond

In socketservices you can use this.socket.ioSocket.nsp = "/mynamespace" only if you have a manually connection.

seba-salavilla avatar Apr 04 '19 15:04 seba-salavilla

Any update?

FaisalAbid avatar Apr 09 '19 19:04 FaisalAbid

"of" is meaningless on the client side. See Socket.io doc: Restricting yourself to a namespace. The @etherealyn's answer above is almost right. Just remplace: { url: 'http://localhost:8080/chat', options: { } } with: { url: 'http://localhost:8080', options: { path: /chat'' } };

atao60 avatar Jun 07 '19 23:06 atao60

I had to use { url: 'http://localhost:8080/chat', options: { } }

mat-codehaus avatar Mar 17 '20 05:03 mat-codehaus

Hi there,

I've achieved namespace like below ,

import { Socket, SocketIoConfig } from 'ngx-socket-io';

export class SocketNameSpace extends Socket{
  constructor(socketConfig: SocketIoConfig){
    super(socketConfig);
  }
}


@Component(........) // metadata goes here
export class MyComponent{
    chat: SocketNameSpace;
    news: SocketNameSpace;

    constructor(private socket: Socket){
        // this.socket is for root
        this.chat  = new SocketNameSpace({url: 'http://localhost:3000/chat',options: {} });
        this.news  = new SocketNameSpace({url: 'http://localhost:3000/news',options: {} });
   }
}

It works fine and awesome!!!

Kuppusamy-R avatar Apr 04 '20 14:04 Kuppusamy-R

I am going to close this as it has been over a year, if you still need to please open up a new ticket

ctfrancia avatar Apr 28 '20 21:04 ctfrancia

I prefer to reimplement of like this

of(namespace: string) {
        const socketInstance = new Socket({url: `${window.location.origin}/${namespace}`})
        this.ioSocketsNameSpaced.push(socketInstance)
        return socketInstance
    }

Because if you don't store all sockets in same service, you have to disconnect independently all of them. An example:

disconnect(close?: any) {
        for (const nsp of this.ioSocketsNameSpaced) {
            nsp.disconnect()
        }
        return this.ioSocket.disconnect.apply(this.ioSocket, arguments)
    }

johaven avatar May 18 '20 17:05 johaven

Hi there,

I've achieved namespace like below ,

import { Socket, SocketIoConfig } from 'ngx-socket-io';

export class SocketNameSpace extends Socket{
  constructor(socketConfig: SocketIoConfig){
    super(socketConfig);
  }
}


@Component(........) // metadata goes here
export class MyComponent{
    chat: SocketNameSpace;
    news: SocketNameSpace;

    constructor(private socket: Socket){
        // this.socket is for root
        this.chat  = new SocketNameSpace({url: 'http://localhost:3000/chat',options: {} });
        this.news  = new SocketNameSpace({url: 'http://localhost:3000/news',options: {} });
   }
}

It works fine and awesome!!!

This solution worked for me. For anyone reading this, don't forget to import SocketIoModule and SocketIoConfig to your app.module.ts, as well as SocketIoModule.forRoot(config) in the imports. After that, you can just create the SocketNameSpace class in whatever component is needed (App.component for me).

eddietal2 avatar Sep 13 '20 17:09 eddietal2

Above solution works great. It should be in the documentation.

yiqu avatar Jan 12 '21 02:01 yiqu

I can look into writing docs for this but I'm not up to date with namespaces in socket.io;

  • Could someone concisely explain to me the issue and proposed community solution in this thread?
  • Are there any actionable points for changes to the code to facilitate the proposed solution, or will just documentation suffice?

Helveg avatar Jan 12 '21 09:01 Helveg

@Helveg I think putting the code example as doc would do the trick.

sucotronic avatar Mar 23 '21 16:03 sucotronic

Ok, so then .of("namespace") should just be done on the client-side by creating a socket that connects to baseUrl + '/namespace' if I understand correctly?

Helveg avatar Mar 23 '21 16:03 Helveg

@Helveg yep, it's something that's user configurable throught the socket options, as noted in https://github.com/rodgc/ngx-socket-io/issues/25#issuecomment-609040721 We can just put mention in the README something like "to use namespaces (link to https://socket.io/docs/v4/namespaces/#Client-initialization) just pass the namespace in the path like the following:".

sucotronic avatar Mar 24 '21 08:03 sucotronic

I used the solution from https://github.com/rodgc/ngx-socket-io/issues/25#issuecomment-479936465, here is the complete example

import { Socket } from 'ngx-socket-io'

export class ChatRoomPage implements OnInit {
  constructor () {
    this.chatSocket = new Socket({
      url: <my-server-url>,
      options: {
        path: '<my-socket-io-base-path>'
      }
    })
    this.chatSocket.ioSocket.nsp = '/chats'
  }

QuentinFarizon avatar Apr 25 '22 15:04 QuentinFarizon

Someone managed to use socket.of ({url: '', options: {}})?

PiotrSik avatar Jul 05 '22 14:07 PiotrSik