Nobody wants to be spammed with notifications. Recently while working on social media app, I ran into that exact problem. Every time a user would get a follow, they would be notified with a simple: “@Username started following you”. Every single time. Of course, some users get hundreds and thousands of new followers every day, so the situation quickly became extremely annoying for them.
After some thought, I decided to create a solution that would group these notifications into one. The resulting code was supposed to let users know something along the lines of "Username1, Username2, and other 7 followed you".
Managing all the moving parts of an app, such as this one, is not easy. So, we needed a scalable, long-term solution. Next came searching for a platform that would help automate the process. We needed a system that would help orchestrate the process and optimise this frequently occurring process. I stumbled across Temporal.io.
The open source platform allows for a reliable and scalable function execution. Solutions built via Temporal are also durable, meaning there is no time limit. Whether my code was supposed to execute for seconds or years, the platform was supposed to help me make it run forever. And here’s how I did it:
Here’s how developing the code for a single user follow went done:
Next was implementing an activity that could execute a send notification code. Workflow code cannot use 3rd party API or have access to the file system. So we need to implement it in activities and to pass a client as a dependancy injection.
Later I had to proxy activities in order to use them in a workflow:
A two hour period was picked for grouping notifications. Use a utility function from Temporal to make a time based condition:
The next step includes adding a worker that will execute tasks from the queue:
Top comments (0)