DEV Community

Cover image for WebSocket, RxJS and other bad things
Ion Prodan
Ion Prodan

Posted on • Originally published at yon.fun on

WebSocket, RxJS and other bad things

There are so many tutorials over the internet about different JS frameworks, API's and technologies, each day I come across to different JS articles, but almost none of them are about RxJS, even more about WebSockets with RxJS!

WebSocket, for more of Javascript developers, is something inexperienced and strange thing, even if they understand how it works, they almost never used it, and it's clear why - not all of them needs to create web-chats! But, if we would look more deeply into this technology, we can understand that WebSocket is not only for "Online Chats" but much more, for example sending binary data to the server or making cool things like this!

These being said, RxJS has the same story, we are used to having it only on Angular projects (for developers who write in Angular - for those who not, I am pretty sure they could never use it) and that's it, I haven't seen any other popular libraries to be implemented it - is it so bad? it weighs so much? it is not reactive enough? I would like to know the answers to these questions πŸ˜’.

What would be the reasons of un-exploring these two things, we are going today to see an example of integrating both of them and see that it is not so bad, even more - I see it more efficient and optimised way to work with WebSockets - of course, we don't talk about simple projects, otherwise is much better to use the WebSocket API.

RxJS WebSocket Package

Listening for messages from the server:

Pushing messages to the server:

All these examples have been taken from RxJS official documentation WebSocket page, so it is available for everyone. If you are a JS developer and haven't tried RxJS yet, it is time to try it, just give it a try - you will see, it'll worth your time.

Begin with surfing this cool website, write some tests, think as Observable, (start writing in TypeScript, get more sleep, go to JS conferences, read JS books and do not stop here) in that way we will build better websites, web-applications, web-servers, mobile-applications and everything which JavaScript allows us to build.

Make the internet a safer and better place.

Top comments (0)