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

Expose a way to switch from one cluster/app to the other #773

Conversation

kamalbennani
Copy link

@kamalbennani kamalbennani commented Nov 8, 2023

What does this PR do?

This PR introduces a way to tell Pusher client to switch the traffic from one cluster/app to the other.

This is super useful especially in case where Pusher is experiencing some outages allowing you to switch the traffic to a stable cluster without having to deal with resubscribing yourself all the channels and bind all the callbacks to the right events.

Usage

const pusher = new Pusher('mt1Key', { cluster: 'mt1' });
const channel = pusher.subscribe('private-channel');
channel.bind('event', console.log);

// Detect pusher outage
pusher.switchCluster({ appKey: "us3Key", cluster: "us3" });

// Here you should expect that all channels and bindings are still working as expected.

Checklist

  • All new functionality has tests.
  • All tests are passing.
  • New or changed API methods have been documented.
  • npm run format has been run

CHANGELOG

  • [Added] Introduce a new .switchCluster method to switch the Pusher client to a different cluster and re-establish all existing subscriptions and channel bindings.

@kamalbennani kamalbennani force-pushed the impr/expose-switch-cluster-func branch from e913a73 to 2e8aaf3 Compare November 8, 2023 21:22
@kamalbennani kamalbennani changed the title [WIP] Expose a way to switch from one cluster/app to the other Expose a way to switch from one cluster/app to the other Nov 8, 2023
src/core/pusher.ts Outdated Show resolved Hide resolved
src/core/pusher.ts Show resolved Hide resolved
src/core/pusher.ts Show resolved Hide resolved
Copy link
Contributor

@MeenaAlfons MeenaAlfons left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @kamalbennani. I've added some comments pointing at necessary changes for the pusher object to be fully functional after switchCluster is called.

src/core/pusher.ts Outdated Show resolved Hide resolved
src/core/pusher.ts Outdated Show resolved Hide resolved
@MeenaAlfons
Copy link
Contributor

I think this PR needs unit testing to verify that all functionalities are working after switchCluster is called.

@kamalbennani
Copy link
Author

Hi @MeenaAlfons Thanks for the review, I've pushed a commit to resolve all your comments.
Let me know if additional changes are needed.

src/core/pusher.ts Outdated Show resolved Hide resolved
src/core/pusher.ts Outdated Show resolved Hide resolved
@kamalbennani kamalbennani force-pushed the impr/expose-switch-cluster-func branch 2 times, most recently from 65ee4b3 to 819be76 Compare November 20, 2023 11:08
@kamalbennani
Copy link
Author

Hi @MeenaAlfons, the PR is ready for another round of review.

@kamalbennani
Copy link
Author

Hi @MeenaAlfons, would you mind reviewing the PR a second time, highly appreciate it!

Copy link
Contributor

@MeenaAlfons MeenaAlfons left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply.

Upon running tests I got some errors:

1) Pusher after connecting switch cluster should resubscribe to all channels
  - TypeError: this.connection.switchCluster is not a function

2) Pusher after connecting switch cluster should send events via the connection manager
  - TypeError: this.connection.switchCluster is not a function

In general, I think the current tests try to mock the behavior or reconnecting by manually calling pusher.connect() and pusher.connection.state = 'connected', etc. This is not helpful when testing switchCluster because the expected behaviour we'd like to test is that the connection will be disconnected and reconnected including all the side effects of reconnection like reinitiating channel authorization and user authentication.

It would be nice if you can make such tests. I could be able to help but it will depend on my availability.

@kamalbennani
Copy link
Author

Hi @MeenaAlfons, I pushed some missing changes which would make the tests pass! sorry for that.

Regarding your comment, I agree with you in theory, and I'd like to know how those tests are done today when a network switch happens, as it's the same logic that is done here.

If that's not cover, I'd prefer if this could be managed by yourself or a pusher member in phase 2 if that's fine with you?

Thanks.

@MeenaAlfons MeenaAlfons changed the base branch from master to releases/v8.4.0-rc1 November 28, 2023 18:43
@MeenaAlfons
Copy link
Contributor

Hi @kamalbennani,

I've changed the base branch to releases/v8.4.0-rc1. The intention is to deploy a release candidate. This will allow you to start using the new version if you need. This will also make it easier to manually test it.

I still don't know when I'll be able to come around to check what needs to be done for tests to cover these changes. I think a release candidate may unblock you.

Thank you.

@MeenaAlfons MeenaAlfons merged commit 32d4a23 into pusher:releases/v8.4.0-rc1 Nov 29, 2023
0 of 2 checks passed
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

Successfully merging this pull request may close these issues.

None yet

4 participants