Understanding RxJS and WebSockets in Angular

WebSocket, for more of Javascript developers, is something inexperienced and strange thing, even if they understand how it works, they rarely used it, and it's clear why - not all of them need web-chats!

Understanding RxJS and WebSockets in Angular

We often stumble upon tutorials daily when exploring JavaScript frameworks, APIs, and technologies. However, a topic not widely covered is the integration of RxJS with WebSockets, a powerful combination beneficial beyond creating web chats.

Why WebSockets? WebSockets offer a real-time communication channel between the client and the server, allowing not just text messages but also cool things like this!

This capability opens the door to various applications beyond simple chats, such as live data feeds, interactive gaming, and collaborative platforms.

The Role of RxJS: RxJS stands for Reactive Extensions for JavaScript, a library for reactive programming using observables. It's commonly used in Angular projects to handle asynchronous data streams effectively.

However, its application seems underexplored outside the Angular realm. 🫤

Making the Connection with RxJS WebSocket Package

RxJS provides a webSocket function that simplifies working with WebSockets.

It allows for both listening to messages from the server and sending messages to the server in a reactive manner. Here are two simple examples derived from the RxJS documentation.

Listening for messages from the server:

import { webSocket } from "rxjs/webSocket";
const subject = webSocket("ws://localhost:8081");

subject.subscribe(
   msg => console.log('message received: ' + msg), // Called whenever there is a message from the server.
   err => console.log(err), // Called if at any point WebSocket API signals some kind of error.
   () => console.log('complete') // Called when connection is closed (for whatever reason).
 );

Listening for messages from the server

For more information about what a subject is in Angular, read this, and what a const is, read this.

Pushing messages to the server:

import { webSocket } from "rxjs/webSocket";
const subject = webSocket('ws://localhost:8081');

subject.subscribe();
// Note that at least one consumer has to subscribe to the created subject - otherwise "nexted" values will be just buffered and not sent,
// since no connection was established!

subject.next({message: 'some message'});
// This will send a message to the server once a connection is made. Remember value is serialized with JSON.stringify by default!

subject.complete(); // Closes the connection.

subject.error({code: 4000, reason: 'I think our app just broke!'});
// Also closes the connection, but let's the server know that this closing is caused by some error.

Pushing messages to the server

These examples show the straightforward nature of using RxJS for WebSocket communication, emphasizing the simplicity and power of reactive programming in handling real-time data.

Embracing Angular and RxJS

JavaScript developers have yet to try RxJS, particularly within Angular projects, so it's worth exploring.

Angular, emphasizing reactive programming and efficient data handling, pairs well with RxJS, enhancing the development of dynamic, real-time web applications.

Starting Tips:

  • Dive into RxJS and understand observables.
  • Practice by integrating WebSockets in a small project.
  • Explore Angular's ecosystem, focusing on its reactive capabilities.
  • Join the Angular community for insights and support.

Conclusion

Combining RxJS with WebSockets opens many possibilities for developing interactive, real-time web applications.

Whether you're building a chat app, a live data dashboard, or any application requiring real-time communication, leveraging RxJS within Angular offers a robust solution.