profiler
profiler

Reputation: 627

Dynamically adding a parameter to socket initialization in ngx-socket-io

I use ngx-socket-io package to implement sockets.

For some implementation reasons, I use a lot of namespaces and need to pass the data parameter from a component to the site build.

I tried to use socket.of(namespace: string) but I can't add options parameter.

@Injectable()
export class UsersListUpdateSocketService extends Socket {

  userListsUpdate$ = this.fromEvent<user[]>('update-users');

  constructor() {
    super({
      url: `${environment.socketUrl}/${SocketEnum.USER_LIST}`,
      options: { data: { parameter: 'value' } }
    });
  }
}

The problem is: I would like, from components, change { data: { parameter: 'value' } } to { data: { parameter: 'value2' } } etc.

Upvotes: 0

Views: 659

Answers (1)

Mahyar8520
Mahyar8520

Reputation: 9

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: {}};

or

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

but below ex awesome:

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: {} });
   }
}

Upvotes: -1

Related Questions