Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

TPDP Episode #8: Asynchronous and Event-Based Programming with RxJS

When it comes to modern JavaScript development, there are a few different ways to handle asynchronous events or data. You could use promises and callbacks, but as great as they are, present certain limitations. This is where RxJS comes into play with its reactive programming model. In this episode of The Polyglot Developer Podcast, guest speaker Ben Lesh and I discuss RxJS and where it fits in modern JavaScript development, whether it be server-side or front-end.

Ben Lesh is a senior software engineer at the very popular entertainment streaming company, Netflix. One of Ben’s projects at Netflix includes the development and maintenance of RxJS since it is heavily used by the company. In the eighth episode, Asynchronous and Event-Based Programming with RxJS we discuss everything from what is RxJS, how it was inspired, who is using it, and why you should use it over a few of the alternative methods. If you’ve ever heard of RxJava or Rx.NET, these projects share some similarities to RxJS.

This podcast can be found for free on Pocket Casts, iTunes, and other popular podcasting networks. If you’d prefer to listen to this episode directly, it can be heard below.

For anyone that would rather read than listen, you can check out the following summary of the episode.

What is RxJS and What Benefit Does it Bring Over Promises and Callbacks?

Rx means reactive extensions, and the JS means that these are reactive extensions for JavaScript. It uses observables which can be considered values over time. The reactive component lets you trigger a chain of reactions to manipulate the values of these observables. These reactions come in the form of operators such as map transformations or filtering. Every time RxJS is notified of a new value in an observable, the line of events that you define are executed.

RxJS works for both front-end and back-end applications. Whether or not you’re using a browser or Node.js, it will work as long as JavaScript is involved. Starting in RxJS 5 you can even use TypeScript which opens the door to more possibilities.

While promises and callbacks are useful, they are limited in what they can do. For example, promises wait for future values and are not lazy. When you receive a future value you can then manipulate it, but you can’t retry the request if it fails and you can’t cancel it if you need to move onto something else. With RxJS you have more power. You can subscribe to an observable or you can unsubscribe from an observable if you wish to cancel it. Observables are lazy so you can chain all your operations together and pass them around. They won’t do anything until you subscribe to them.

Who is Currently Using RxJS?

RxJS is heavily used by Netflix, the company that is actively contributing to the project. They need to be able to quickly and efficiently work with large streams of data and manipulate it as necessary. While Ben doesn’t explain the secret recipes on how Netflix is using it, he mentioned that it is a critical part of their business.

One of my current favorite frameworks, Angular, is using RxJS as a core dependency. I’ve stumbled upon RxJS when trying to do HTTP requests with Angular because the responses can be transformed using the map operator and then further subscribed to using the subscribe operator. However, HTTP is only a small piece to the Angular puzzle. While RxJS is used throughout the project, a great example he gave was in navigation. Because RxJS events can be canceled, unlike promises, outstanding events can be canceled during navigation. So if a user navigates somewhere, triggers a flow of events, then navigates away, the flow of events can be stopped instead of continuing to run. This adds performance gains to the application being developed.

Conclusion

I’ve only provided a summary of the things we discussed in the podcast episode. RxJS is a powerful beast. As someone who came from RxJava, I already had some familiarity with the subject and how convenient it is to use reactive extensions. Since observables will one day be added to JavaScript proper, it is likely that RxJS becomes a more significant part of JavaScript development. Netflix is already very invested in it and they have heavy usage with high performance demands.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Search

Follow Us

Support This Site