Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't disconnect from Pusher #94

Closed
steve-rhodes opened this issue Jan 22, 2017 · 20 comments
Closed

Can't disconnect from Pusher #94

steve-rhodes opened this issue Jan 22, 2017 · 20 comments

Comments

@steve-rhodes
Copy link

steve-rhodes commented Jan 22, 2017

Pusher has the disconnect() method. Is there such a thing in Echo? I couldn't see it. It's very important for me to disconnect users who keep their tab(s) open otherwise I run out of connections.

@RusinovAnton
Copy link
Contributor

You can use ".leave()" method which is described in the doc

@steve-rhodes
Copy link
Author

But that's not the same. You only leave a channel with this, which still keeps the Pusher connection open, I saw. Only pusher.disconnect() will close it, which is not available through Echo.

@RusinovAnton
Copy link
Contributor

@elasticsteve It would be really useful if you could give your use case as example.

@steve-rhodes
Copy link
Author

I said it in my first comment: to disconnect users keeping their tabs open. It's a common problem with Pusher, because it eats up all your available connections and you max out.

@RusinovAnton
Copy link
Contributor

@elasticsteve I mean, could you write some pseudo code implementation of that. How would you use .disconnect() if it were implemented. Will you need .reconnect() ?
The approach I'm thinking on right now is to disconnect from Pusher on .leave() call without channel name argument

Echo.leave() // this.pusher.disconnect()
Echo.leave('my-channel') // his.channels[name].unsubscribe()

Whats your opinion on this?

@steve-rhodes
Copy link
Author

You open a connection with new Echo();

At that moment Pusher shows an active connection, even if there is no listener subscribed. We need something to close that connection again, so Pusher releases the connection. On my account I have a max of 200 connections, but if people just leave their tabs open on a computer that is on all day or at work, I run out of connection within hours. Pusher doesn't disconnect. Even worse if one user has my app on tabs, they eat up 4 connections! You can see how soon you run out.

I think Echo. disconnect(); would be brilliant for that, similar to pusher.disconnect()

@RusinovAnton
Copy link
Contributor

RusinovAnton commented Jan 22, 2017

Ok Im going to show you how I use Echo with ReactJS for my app and why I'm concerned about adding .disconnect() wouldn't be that easy as it seems:

// AppEchoInstance.js

import Echo from 'laravel-echo'

export default new Echo({/* my app pusher config */})

-----------------
// NotificationsComponent.js

import AppEchoInstance from 'AppEchoInstance'

...

componentDidMount() {
  // Using single instance here I'm having only one connection per page, not multiple if I'm not wrong
  AppEchoInstance
    .channel('my-channel-1')
      .listen('event1', () => {})
      .listen('event2', () => {})
      .listen('event3', () => {})

  AppEchoInstance
    .channel('my-channel-2')
      .listen('event4', () => {})
      .listen('event5', () => {})
}

componentDidUpdate() {
  if (subscribeToOneMoreChannel) {
     AppEchoInstance
      .channel('my-channel-3')
        .listen('event6', () => {})
        .listen('event7', () => {})
  }
}

...

-----------------
// MainComponent.js

import AppEchoInstance from 'AppEchoInstance'

...

// User leaves tab
window.onblur = function() {
  AppEchoInstance.disconnect() // ok we did it
}

// User comes back to this tab
window.onfocus = function () {
 //  What we do here then?
 //  AppEchoInstance.disconnect()
}

...

So what I have to do with my shared Echo instance when user comes back to the tab? I think I want to call .reconnect() on my instance i guess? Then I need to resubscribe to all channels I have in this instance?

@steve-rhodes
Copy link
Author

I would't use it like this this. It's too extreme. I would use a timer tracking long inactivity.

Of course the problem is that mobile phones just stop javascript when the tab is not active or the browser hidden. I heard there are new way, but I'm not familiar with it. There whole thing became such a nightmare for me that I actually don't use Pusher / Echo now. It's not essential for my app, but it would have been nice for some real time updates and messages. Since they are not essential for my app, it's not worth doing right now, because I would run out of max connections all the time and it would ruin everything. Unless we find a better way of dealing with inactive user tabs or even hidden browsers, I can't really see Pusher going very far in terms of mainstream, where the real time websocket updates are not a "must", but rather an expensive pain in the neck.

@RusinovAnton
Copy link
Contributor

@elasticsteve thanks for sharing your experience tho. I think that disconnect should be added after all but I want someone from echo team to share their thoughts on its implementation first.

@RusinovAnton
Copy link
Contributor

var echo = new Echo()

echo.connector.pusher // this is where instance of Pusher is.

I guess you could try to call .disconnect() on it.

@steve-rhodes
Copy link
Author

Thanks, I will. How would I re-connect again?

@RusinovAnton
Copy link
Contributor

@elasticsteve at this point I guess there is nothing better than call new Echo() again

@steve-rhodes
Copy link
Author

ok, just wanted to be sure it's the right thing to do.

@maxpaynestory
Copy link
Contributor

@elasticsteve I have added the disconnect functionality inside my pull request. Hope it helps

@steve-rhodes
Copy link
Author

Great!

@maxpaynestory
Copy link
Contributor

@elasticsteve Pull request just got merged. Could you please test it and let me know if it works for you.

@steve-rhodes
Copy link
Author

@maxpaynestory Sorry, I'm not using Pusher anymore in the app I'm developing. We will need it later (in a few months again) for new features.

@maxpaynestory
Copy link
Contributor

then i guess this issue can be closed?

@mcblum
Copy link

mcblum commented Nov 24, 2017

@maxpaynestory Hey -- just wanted to say he may have not used this but I really appreciate your adding it! We have an app where a user can be part of multiple companies, so when they switch companies we need to completely disconnect them from the other company's pusher channel and resub. This saved the day.

@mcblum
Copy link

mcblum commented Nov 24, 2017

@maxpaynestory actually, I may have spoken too soon :) I'm not seeing the disconnect method in the suggestions nor is it disconnecting from Pusher. If you have any ideas, let me know!

Edit: Nope, I did not, it works exactly as expected. Just had some cached code or something. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants