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

#576 Drop dependencies to Primus and Primus plugins #1052

Merged
merged 62 commits into from
Apr 8, 2021

Conversation

snowteamer
Copy link
Collaborator

@snowteamer snowteamer commented Feb 27, 2021

Closes #576

Summary of changes:

  • Drops Primus.js and the primus-rooms and primus-responder plugins in favor of a pubsub implementation using only the ws socket library in Node and the WebSocket API in the browser.
  • Increased the initial WebSocket connection timeout from 3000 to 5000 ms (Primus defaults to 10000 ms).
  • Correction of a bug that often caused redundant sub/unsub requests.
  • Less verbose backend console logging (from 19 k lines of build output to ~10 500 lines).
  • New 'backend/server/broadcastEntry' SBP selector.

Additional information:

The core of this proposed pubsub framework consists of only two files:

  • ~/backend/utils/pubsub.js which exports a createServer() function.
  • ~/shared/pubsub.js which exports a createClient() factory suitable to create either ordinary pubsub clients in the browser or server-side clients in Node.
    Such server-side clients can be useful e.g. in testing or to talk to other servers.

Currently, it comes with a set of so-called "default" event handlers, designed to handle only basic pubsub protocol stuff and ENTRY messages as required by the application.

However this protocol could be extended by providing appropriate "custom" message handlers to the factories.

In progress:

Primus has automatic reconnection features which are not all implemented in this PR yet:

  • Reconnect upon initial connection timeout
  • Reconnect when the server restarts
  • Reconnect when the online event occurs, including in case the web frontend was started in offline mode. It seems to be working now, but again tests are still missing.
  • Client-side connection loss detection independently of the offline/online events using ping/pong messages (https://github.com/primus/primus#heartbeats-and-latency).

All these features are options so they can be disabled if we do not need them.

Again, testing these features might be tricky, so any help or advice will be much appreciated.

Copy link

@joepie91 joepie91 left a comment

Choose a reason for hiding this comment

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

Initial review; it's end of day for me now, I will likely post further review comments later :)

backend/server.js Outdated Show resolved Hide resolved
backend/utils/pubsub.js Outdated Show resolved Hide resolved
backend/utils/pubsub.js Outdated Show resolved Hide resolved
backend/utils/pubsub.js Outdated Show resolved Hide resolved
backend/utils/pubsub.js Outdated Show resolved Hide resolved
backend/utils/pubsub.js Outdated Show resolved Hide resolved
backend/utils/pubsub.js Outdated Show resolved Hide resolved
@snowteamer snowteamer force-pushed the remove-primus branch 2 times, most recently from 20b2ab0 to f875568 Compare February 28, 2021 23:38
Copy link
Member

@taoeffect taoeffect left a comment

Choose a reason for hiding this comment

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

@snowteamer this is a really fantastic PR you've sent in! Because of its size, it will take me a bit longer to review fully, however I've gone ahead and left some feedback for now so that you can work on it / respond to it while I continue to review.

.Gruntfile.babel.js Show resolved Hide resolved
.Gruntfile.babel.js Show resolved Hide resolved
frontend/main.js Outdated Show resolved Hide resolved
shared/pubsub.js Show resolved Hide resolved
shared/pubsub.js Show resolved Hide resolved
shared/pubsub.js Show resolved Hide resolved
shared/pubsub.js Outdated Show resolved Hide resolved
shared/pubsub.js Outdated Show resolved Hide resolved
@snowteamer snowteamer requested a review from taoeffect March 23, 2021 04:58
@snowteamer snowteamer force-pushed the remove-primus branch 2 times, most recently from bb9de23 to 640f6f1 Compare March 27, 2021 14:25
shared/pubsub.js Outdated Show resolved Hide resolved
Copy link
Member

@taoeffect taoeffect left a comment

Choose a reason for hiding this comment

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

@snowteamer OK this is my final review I think. Only minor things remaining, posted comments, plus a request to use okTurtles.events where possible unless there's a reason not to. You're already using it in some parts of the code, I'm not sure why we need to re-implement event emission, and if there isn't a strong reason to, let's not do that.

backend/pubsub.js Outdated Show resolved Hide resolved
backend/pubsub.js Show resolved Hide resolved
backend/pubsub.js Show resolved Hide resolved
backend/pubsub.js Outdated Show resolved Hide resolved
backend/server.js Show resolved Hide resolved
frontend/main.js Outdated Show resolved Hide resolved
frontend/main.js Outdated
sbp('okTurtles.data/set', PUBSUB_INSTANCE, createGIPubSubClient(
pubsubURL, {
// This option can be enabled since we are not doing auth via web sockets.
reconnectOnTimeout: true
Copy link
Member

Choose a reason for hiding this comment

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

Here we lost the timeout value of 3000, could that be added back in please (especially since the default appears to be 5000)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That was on purpose, 3000 is likely too short:

  • We have seen that timing issues can cause the Travis build to fail.
  • All web socket libraries that I have checked use a higher default timeout or do not seem to specify any at all:

Copy link
Member

@taoeffect taoeffect Apr 6, 2021

Choose a reason for hiding this comment

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

ok, it's still useful to specify it here though to remind us that's an option and what its current value is without diving into the implementation. To your first point, we can also extend it for travis, e.g. something like this:

timeout: process.env.NODE_ENV === 'development' ? 10000 : 5000 // extend for Travis

@snowteamer
Copy link
Collaborator Author

Unfortunately crypto.randomBytes() doesn't seem to exist in the browser, so I will likely have to revert tocrypto.getRandomValues() :(

@taoeffect
Copy link
Member

Unfortunately crypto.randomBytes() doesn't seem to exist in the browser, so I will likely have to revert tocrypto.getRandomValues() :(

Ok, I replied with a suggestion on Slack

Copy link
Member

@taoeffect taoeffect left a comment

Choose a reason for hiding this comment

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

Approved! Excellent work @snowteamer! 😄 🥇 🎉

@taoeffect taoeffect dismissed joepie91’s stale review April 8, 2021 22:37

Resolved, afaik?

@taoeffect taoeffect merged commit ccd30db into okTurtles:master Apr 8, 2021
@snowteamer snowteamer deleted the remove-primus branch April 8, 2021 22:52
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.

Remove primus dependency [$3k bounty]
3 participants