Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building scalable breakout rooms with Node.js worker threads

Building scalable breakout rooms with Node.js worker threads

NodeJS worker threads have opened doors to many exciting implementations and functionalities on the server-side. Multiplayer networked games, and live engagement and collaboration, among others, make a really good candidate for CPU intensive applications which would greatly benefit from this multi-threaded-ness of NodeJS.

In this talk, we’ll understand everything about NodeJS worker threads and see a real implementation of this concept to simulate dedicated server instances to enable various groups of players to play private instances of a live quiz simultaneously.

Srushtika Neelakantam

February 25, 2021
Tweet

More Decks by Srushtika Neelakantam

Other Decks in Programming

Transcript

  1. Let’s see breakout rooms in action TIME FOR A FUN

    QUIZ! 3 @Srushtika | Brighton Web Dev Meetup | https://quiz.ably.dev/
  2. BREAKOUT ROOMS FUNCTIONALITY 1. How does realtime messaging work? 2.

    How to make use of Node.js worker threads to efficiently simulate dedicated server instances for each breakout room? @Srushtika | Brighton Web Dev Meetup |
  3. Publish/Subscribe messaging pattern Feature add-ons • Presence • Message History

    • Integrations . . . Infrastructure add-ons • Auto reconnection • Scale • Guaranteed delivery • Message ordering . . . P.S. This is an old gif, Ably has moved on to new branding now. @Srushtika | Brighton Web Dev Meetup |
  4. BREAKOUT ROOMS FUNCTIONALITY 1. How does realtime messaging work? ✅

    2. How to make use of Node.js worker threads to efficiently simulate dedicated server instances for each breakout room? @Srushtika | Brighton Web Dev Meetup |
  5. KEY ELEMENTS THAT MAKE NODE.JS WORK The chrome v8 engine

    converts JavaScript to machine readable code The libuv library provides and manages the event loop and thread pool @Srushtika | Brighton Web Dev Meetup |
  6. GREAT EXPLANATION OF EVENT LOOP BY PHILIP ROBERTS (JSConfEU on

    YouTube) @Srushtika | Brighton Web Dev Meetup |
  7. “Node.js is a CPU bottleneck due to its single-threaded nature.

    That’s one of its major drawbacks” @Srushtika | Brighton Web Dev Meetup |
  8. BREAKOUT ROOMS FUNCTIONALITY 1. How does realtime messaging work? ✅

    2. How to make use of Node.js worker threads to efficiently simulate dedicated server instances for each breakout room? ✅ @Srushtika | Brighton Web Dev Meetup |
  9. CAVEATS AND OTHER THINGS - Don’t use Worker threads for

    parallelizing I/O operations, the native Node architecture can do that more quickly - Creating worker threads is not cheap, so it’s better to create and use a thread pool instead - If there are async methods available, it’s better to use them, rather than off-loading to your own worker thread @Srushtika | Brighton Web Dev Meetup |
  10. Thankyou! @Srushtika | Brighton Web Dev Meetup | Further reading

    - NodeJS docs: https://nodejs.org/api/worker_threads.html - Demo source code: https://github.com/Srushtika/realtime-quiz-framework - “What the heck is an event loop anyway?”: https://youtu.be/8aGhZQkoFbQ - “Introduction to libuv: What's a Unicorn Velociraptor?”: https://youtu.be/_c51fcXRLGw - Realtime concepts: https://ably.com/topics