In Part-2, we have seen our web app fetching data from graph database via an API layer. Now, users can get the movies info of their favorite actors or the cast of their favorite movies using the web form with out much latency.
In this post, we will explore streaming these results back to the server and in the process get our hands dirty with Web Sockets and Kafka Streams. Following video demonstrates the learning outcomes of this post with respect to Sockets & Streams.
As far as my experience goes working with Web Sockets in multiple languages - Python, Rust and NodeJs, NodeJs websocket library - ws - is more amicable to work with especially in seamless integration with web client frameworks such as Angular. And, thanks to Microservices Based Architecture with Docker, it is ever so easy to glue together systems of different languages cohesively so that we are using best of the benefits programming language ecosystem has to offer.
With that being said, let’s add stream functionality using web sockets to our UI codebase.
services/ws.service.ts
app.component.html
app.component.scss
app.component.ts
It’s time to fire up NodeJs container and serve our server side application that captures the stream events from our client and publishes the events in turn onto Kafka topic.
Once on the container,
Add the following content to /mnt/web-sock-app/index.js.
Run the application using,
node index.js
Disclaimer:
In a production app, it’s recommended to use key-value based messages onto kafka topic. In doing so, with proper hash function, identical message will always be sent to the same partition of the kafka topic. In addition, it’s best advised to use Apache Avro format when serializing the messages onto Kafka Topic.
If you don’t have Kafka Cluster set up, please refer to this post. Go ahead and create test-topic and you should see the messages getting streamed onto the consumer as you play with the IMDB form on our web-ui.