Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsubscribe/Disconnect the real time update JS library

Tags:

getstream-io

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()
like image 932
user391986 Avatar asked Dec 13 '25 02:12

user391986


1 Answers

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();
like image 166
Matthisk Avatar answered Dec 15 '25 14:12

Matthisk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!