0

The Client class of STOMP has a method called deactivate(). Is it going to terminate all active WS connections, or do I need to manually unsubscribe from subscriptions made inside the onConnect callback?

public setWebSocketConnections(loggedUser) {
    let scope = this;
    console.info('Obtaining WS connection...');

    this.stompClient = new Client({
      webSocketFactory: () => new SockJS(scope.runtimeEnvironment.WEBSOCKET_ENDPOINT),
      connectHeaders: {
        'ctx-jwt': loggedUser.temporaryToken.match(/ctx-jwt=[^;]+/)[0].split('=')[1]
      },
      onConnect: frame => this.onConnectionSuccess(scope),
      reconnectDelay: 10000,
      heartbeatIncoming: 10000,
      heartbeatOutgoing: 10000
    });

    if (!this.runtimeEnvironment.PRODUCTION)
      this.stompClient.debug = console.log;

    this.stompClient.activate();
  }

  private onConnectionSuccess(scope) {
    let liveContextFn = response => this.liveContextInProgress.next(response);

    // Live context modification endpoint
    this.subscriptions.push(scope.stompClient.subscribe('/topic/contexts/live', message => liveContextFn(JSON.parse(JSON.parse(message.body).content))));
  }

  // Closes all active connections of the logged user
  public disconnect() {
    this.subscriptions.forEach(s => s.unsubscribe()); // Is this needed?

    if (this.stompClient)
      this.stompClient.deactivate();
  }
Diego Victor de Jesus
  • 2,575
  • 2
  • 19
  • 30

1 Answers1

0

Generally speaking, explicitly removing a subscription that you explicitly created is the "right" thing to do.

When your client creates a subscription it forces the STOMP server to track that state in some way. Depending on the semantics of the subscription (some STOMP servers support special subscription features not directly outlined in the specification) it may be removed when the client closes its connection. Otherwise the broker may retain that state and remove it later or in the worst case it will keep it forever and eventually run into resource utilization issues (e.g. run out of memory).

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43