Slide 1

Slide 1 text

BUILDING SCALABLE BREAKOUT ROOMS USING NODE.JS WORKER THREADS Srushtika Neelakantam Developer Relations Engineer at Ably Realtime

Slide 2

Slide 2 text

HELLO! I’M SRUSHTIKA DevRel Engineer @Srushtika

Slide 3

Slide 3 text

Let’s see breakout rooms in action TIME FOR A FUN QUIZ! 3 @Srushtika | Brighton Web Dev Meetup | https://quiz.ably.dev/

Slide 4

Slide 4 text

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 |

Slide 5

Slide 5 text

HTTP WebSockets vs. @Srushtika | Brighton Web Dev Meetup |

Slide 6

Slide 6 text

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 |

Slide 7

Slide 7 text

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 |

Slide 8

Slide 8 text

WORKER THREADS IN NODE.JS @Srushtika | Brighton Web Dev Meetup |

Slide 9

Slide 9 text

HOW DOES NODE.JS WORK? @Srushtika | Brighton Web Dev Meetup |

Slide 10

Slide 10 text

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 |

Slide 11

Slide 11 text

GREAT EXPLANATION OF EVENT LOOP BY PHILIP ROBERTS (JSConfEU on YouTube) @Srushtika | Brighton Web Dev Meetup |

Slide 12

Slide 12 text

“Node.js is a CPU bottleneck due to its single-threaded nature. That’s one of its major drawbacks” @Srushtika | Brighton Web Dev Meetup |

Slide 13

Slide 13 text

WORKER THREADS IN NODE.JS @Srushtika | Brighton Web Dev Meetup |

Slide 14

Slide 14 text

NODE.JS WORKER THREADS ARCHITECTURE @Srushtika | Brighton Web Dev Meetup | Image source: The NodeSource Blog

Slide 15

Slide 15 text

@Srushtika | Brighton Web Dev Meetup |

Slide 16

Slide 16 text

Let’s see some real code https://github.com/ably-labs/realtime-quiz-framework @Srushtika | Brighton Web Dev Meetup |

Slide 17

Slide 17 text

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 |

Slide 18

Slide 18 text

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 |

Slide 19

Slide 19 text

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