Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.
I wrote previously about how you can set up multiple observable sequences and subscribe to them with multiple observers and create a many-to-many relationship between them.
Whilst this is a very flexible model with a clear separation of responsibilities, often it requires more work to set up and is more than what you need for the task at hand. For instance, if you’re receiving a steady stream of inputs and want to log the arrival of new inputs as well as performing some aggregation on them, you don’t necessarily have to create two subscribers for the input but instead make use of the Rx.Observable.Do function.
Like the Rx.Observable.Subscribe function, the Do function can take a subscriber, or two up to three function objects to handle the onNext, onError and onCompleted events, in that order. Unlike the Rx.Observable.Select function, it doesn’t have a return value and therefore won’t allow you to transform the input stream, it’s intended purely for causing side effects.
I’ve put together a quick demo (see below) to illustrate the use of the Do function in conjunction with other common RxJS functions such as Select and Where. For this demo we just need two <span> elements, one to show the running total, the other to show a log message every time a new value is received:
Couple of things to note here:
- line 13 – this is a deliberate attempt to give the observable sequence a random chance of excepting to invoke the onError handler specified in the Do function on line 30
- line 28 – the Do function updates the HTML content in the log <span> element every time it receives a new value
- line 32 – show a different log message when we have exhausted the observable sequence
- line 34 – apply a filter on the observable sequence for all subsequent functions
- line 36 – only odd numbers are fed to this handler
If you are lucky enough (or unlucky enough depending on which scenario you’re trying to test) just refresh the page and try again and hopefully you have better luck the second time around!
I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.
Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.
Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!
Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!
Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Just how expensive is the full AWS SDK?
- Many faced threats to Serverless security
- We can do better than percentile latencies
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong
- Top 10 Serverless framework best practices