3

We are dealing with a SPA (single page application) so when the user logs in the UI refreshes without a page refresh. We also initiate the stream socket connection through the library like below. Now when the user logs out we want to destroy this. We tried user1.unsubscribe() and client.disconnect() but doesn't seem to work eventhough method looks defined in the source.

What's the right way to handle this in an environment where the page never refreshes?

  client = Stream.connect('xxx', null, 'xxx')
  user1 = client.feed('notification', @user.get('id'), @user.get('streamToken'))

  callback = (data) =>
    return

  failCallback = (data) ->
    return

  user1.subscribe(callback).then (->
  ), failCallback

  @user.on 'logged:out', ->
    user1.unsubscribe()
    client.disconnect()
user391986
  • 29,536
  • 39
  • 126
  • 205

1 Answers1

3

You have to capture the binding returned from user1.subscribe, on this object you can call unsubscribe to cancel the realtime subscription. See the Faye section of the Stream-js library. Example code:

var subscription = user1.subscribe(callback).then(function() {}, function() {});
subscription.cancel();
Matthisk
  • 1,501
  • 10
  • 20
  • `var subscription = user1.subscribe(callback);` `subscription.then(function() {}, function() {});` `subscription.cancel();` – Sergey Jun 05 '17 at 12:20