Search code examples
websocketngxs

Ngxs + WebSocket | How to listen the socket events at client?


I am using Ngxs/websocket-plugin in my project.

I connected the WebSocket with

this.store.dispatch(new ConnectWebSocket());

and dispatching the socket messages with

sendMessage(from: string, message: string) {
    const event = new SendWebSocketMessage({
      type: 'message',
      from,
      message
    });

    this.store.dispatch(event);
  }

The socket events are being received on the server but I am not getting how to listen to the socket event at the client?


Solution

  • The socket messages are being dispatched in the store by the ngxs/websocket-plugin.

    This plugin will receive a message from the server and dispatch the message as an action with the corresponding type value. If the type property doesn't match any client-side @Action methods (with an Action with the corresponding static type property value) then no State will respond to the message.