<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ujjwal Jindal on Medium]]></title>
        <description><![CDATA[Stories by Ujjwal Jindal on Medium]]></description>
        <link>https://medium.com/@jindalujjwal0720?source=rss-d71bb8057a4a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dfSkLrs3zQao8xZSzHeAHA.jpeg</url>
            <title>Stories by Ujjwal Jindal on Medium</title>
            <link>https://medium.com/@jindalujjwal0720?source=rss-d71bb8057a4a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:56:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jindalujjwal0720/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[API-First AI Agentic Patterns: Building Smarter Systems Without the Framework Overhead]]></title>
            <link>https://levelup.gitconnected.com/api-first-ai-agentic-patterns-building-smarter-systems-without-the-framework-overhead-1a0bac2f1b04?source=rss-d71bb8057a4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a0bac2f1b04</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[agents]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[prompt-engineering]]></category>
            <dc:creator><![CDATA[Ujjwal Jindal]]></dc:creator>
            <pubDate>Wed, 19 Mar 2025 14:14:06 GMT</pubDate>
            <atom:updated>2025-03-19T14:14:06.311Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-CIHaNQTv_8KGeae" /><figcaption>Photo by <a href="https://unsplash.com/@julientromeur?utm_source=medium&amp;utm_medium=referral">julien Tromeur</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote>We’ve worked with dozens of teams building LLM agents across industries. Consistently, the most successful implementations <strong>use simple, composable patterns</strong> rather than complex frameworks. — <a href="https://www.anthropic.com/engineering/building-effective-agents">Erik Schluntz and Barry Zhang</a></blockquote><p>Let’s get real — frameworks are like those hipster, overly priced coffee houses. Yes, they’re cosy and make you feel part of something cool, but do you want a barista to pour milk into your coffee? No way. You can make it stronger, quicker, and just how you want it at home without additional expenses or waiting in line.</p><p>The same thing can be said for constructing AI agents. You don’t necessarily have to use LangChain, Haystack, or some other heavy-duty drag-and-drop library to get the job done. You need direct API access, a dash of ingenuity, and a cup of coffee to keep you awake.</p><p>Borrowing a leaf from <a href="https://www.anthropic.com/engineering/building-effective-agents">Anthropic’s latest blog entry on AI agentic patterns</a> (huge thanks to their team for the idea), I set out to take matters into my own hands and construct my autonomous systems — no frameworks, no superfluous layers, just pure API-driven wizardry. And you know what? It’s not only feasible, it’s supremely empowering.</p><h4>Why Should You Care About Agentic Patterns?</h4><p>Let us take a second before we get down to the nitty-gritty and tell us why agentic patterns are necessary in the first place.</p><p>AI agents are fantastic, but agentic patterns? That’s pure magic. You can <strong><em>imagine agentic patterns as blueprints for creating intelligent, autonomous systems that you can use repeatedly</em></strong>. Rather than constructing single-use bots that do something and you don’t see them again, you’re designing workflows that learn, get better, and even improve over time.</p><p>For instance, consider a customer service representative answering FAQs, reviewing past cases, searching for complaint patterns, and generating product development ideas on their initiative. A data processing agent can also discover anomalies and predict trends that become actionable insights without continuous human intervention.</p><p>The strength of agentic patterns is in their scalability and flexibility. They enable you to create systems that scale with you instead of being bogged down by the limited scale of current frameworks.</p><h4>Why Go Framework-Free?</h4><p>And so, you may ask: <em>If there are frameworks to simplify our lives, why shun them?</em></p><p>In reality, frameworks are helpful — they abstract complexity, offer pre-existing components and usually possess strong community backing. However, they also have compromises to make. Frameworks are also bloated, rigid, and sometimes too much of an overkill for your attempt. They add excess overhead, <strong>hide what’s going on underneath the hood</strong>, and restrict your capacity to tailor your system.</p><p>You get complete control over your AI agents by going frameworks and depending on direct APIs. You can optimise for performance, customise the system to your requirements, debug easily when needed, and know precisely how everything functions. It’s like having your coffee machine rather than depending on Starbucks — you can choose the strength, flavour, and timing without middlemen.</p><p>We must first see the fundamental blocks that make us comprehend the agentic patterns. The blog post by Anthropic explains these patterns, and we will apply them with the OpenAI SDK and NodeJS to communicate with the LLMs directly.</p><h3>Building Blocks</h3><p>The entire purpose of AI agents — or what I prefer to refer to as <strong>agentic workflows </strong>— is to get our computers to do the heavy work for us. We don’t merely want them to sit there and instruct us on what to do; we want them to roll up their sleeves and get it done. Imagine having a personal assistant who doesn’t give you a to-do list but checks off the items for you. That’s the dream. Right?</p><p>To achieve this, we must take a step back and consider the way that we, as humans, do work. We don’t suddenly know everything — we look for information, act, and learn from past actions. We’re halfway there if we can provide AI agents with essential capabilities — looking(searching), performing functions(tools), and recalling past actions(memory).</p><p>This is where the concept of <strong>augmenting LLMs</strong> comes into play. Large language models are interesting right out of the box, but they’re not ready to dominate the world yet. They’re more like a bright intern with much knowledge but require mentoring to accomplish things. We can make them complete agents who can act independently and simplify our lives by providing them with select superpowers.</p><h4>What Does It Mean to Augment an LLM?</h4><p>Augmenting an LLM means giving it more than just the ability to generate text. It’s about transforming it from a passive conversationalist to an active problem solver.</p><p>Deal with it — no human or even an LLM can be omniscient. By having your agent access the web or question a database, you’re making the totality of world knowledge at hand. Need to view current stock prices? Must pull up a customer’s purchase history? An agent with searching capabilities can accomplish it in seconds.</p><p>A talk-only LLM is comparable to a chef who can write recipes but can’t cook. To be valid, it will have to be able to do things. That means adding APIs and functions that allow the agent to perform actions — send an email, update a spreadsheet, or even control smart home devices like IoT applications.</p><p>Aside from those, one of the most substantial limitations of vanilla LLMs is that they possess no memory. They’re akin to goldfish — every encounter is scratch-level. In memory, your agent will remember past actions, learn from them, and build context over time. This becomes key to being able to handle advanced, multi-step workflows.</p><h4>Basic LLM Calls</h4><p>Okay, now let’s move on to the exciting part — coding. Assuming you know how to invoke LLMs if you’re here. But to go through the basics, I’ll give you a brief rundown to get started. There are no frills, only the bare necessities.</p><p>Invoking an LLM directly is as simple as it comes. Whether you’re calling OpenAI, Hugging Face, Anthropic, or some other provider, the steps are essentially the same: authenticate, submit a prompt, and receive a response.</p><p>For instance, with OpenAI, you will install your API key, create your prompt, and issue a completion call. Hugging Face? Loading a model, feeding it some text, and letting it spit out is even easier. Anthropic creations are similar, with some modifications to their API format.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c8f4784d2201891b12f703e5e0a4e01/href">https://medium.com/media/8c8f4784d2201891b12f703e5e0a4e01/href</a></iframe><p><em>Don’t get me wrong when employing Gemini as the LLM; it offers a rich free tier and is acceptable for building and experimenting with agents.</em></p><h4>Structured Output</h4><p>Let’s discuss something underrated but worthwhile: <strong>structured output</strong>. You don’t always have to get a wall of text back when working with LLMs. You can have places where you’d like something cleaner, more predictable, and more actionable — like JSON, XML, or a simple good old-fashioned list. That’s where structured output appears.</p><p>Consider it. You cannot accept sloppy text if you build an AI agent that makes choices, reads information, or communicates with other computers. You require clean <em>machine-readable output</em> to proceed into the next phase of your pipeline.</p><p>For instance, if you are creating a weather bot, rather than the LLM returning with, “<em>It will be sunny with a high of 75 degrees</em>,” it could respond with something like:</p><pre>{<br> &quot;weather&quot;: &quot;sunny&quot;,<br> &quot;temperature&quot;: 75,<br> &quot;unit&quot;: &quot;F&quot;<br>}</pre><p>This structured output makes it easy to parse, store, or use in downstream tasks. There is no need to write complex regex or natural language processing logic to extract the information — it’s already neatly organized for you.</p><p>All existing LLMs have out-of-the-box structured output support. You can prompt the model to return answers in a specific form by modifying your prompts or through features like function calls (OpenAI) or customized templates. Guide the model in the proper direction and say: “<em>Hey, don’t just chat — format your responses like so.</em>”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/908a8a88d4d878f027ffabeca06b4a92/href">https://medium.com/media/908a8a88d4d878f027ffabeca06b4a92/href</a></iframe><p>It is more about optimising and innovating your AI agents. The better and more optimised your predicted outputs are, the better your agents can perform more complex tasks, fit well into other systems, and bring more value.</p><h4>Invoking Tools</h4><p>AI note-taking agents are similar to text-producing AI agents. Naturally, they’re helpful but are not rolling up their sleeves and getting it accomplished. It’s here that the tools are utilized. It makes your agent greater than merely a passive observer but an active one.</p><p>Tools are out-of-band actions or APIs your agent can invoke to cause something to happen. Need to read from a database? Tools for that. Want to send an e-mail or fill out a spreadsheet? Got it; tools can do all of those things, too. They’re the interstitial between your agent’s abstractions and the physical world.</p><p>For instance, if you’re building a customer support agent. Rather than simply making suggestions, it could:</p><ul><li>Pull a customer’s purchase history from an API.</li><li>Check inventory in real-time.</li><li>Follow-up/refund — all done automatically.</li></ul><p>The magic here, herein, is integration. Connecting your LLM to these sites enables it to think and act. It’s as if you’re moving from a GPS that tells you where to go to one that can drive the car for you (concept of Tesla?).</p><p>The majority of contemporary LLMs implement this type of functionality. You specify the tools and what you want them to do, and your agent determines when and how they are implemented. It is not writing out each step but providing your agent with a way to create creative solutions.<br>Plug in the necessary tools and allow your agent to do the rest. When your agent is able not only to speak but to act as well, that is when it is made strong.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c89800ebeb836d532cfefe9159550f92/href">https://medium.com/media/c89800ebeb836d532cfefe9159550f92/href</a></iframe><p>When we refer to calling tools, it is not merely calling APIs or functions. It also makes your agent search for a knowledge base, database, or vector store. Consider it as one additional tool in your agent’s toolbox, such as sending an email or retrieving data.</p><p>For instance, consider your agent answering a customer question such as, “Where is my order?” Rather than trying to guess or fabricate, it can look up the knowledge base about the user’s order, retrieve the correct information, and apply that to create an accurate response. It’s not much different from invoking a function or employing RAG. It’s sketching the proper information at the appropriate moment. The agent is starving; it fetches it via an API, database, or document. When you integrate KB search with other technology, your agent isn’t merely answering questions; it’s resolving issues, making decisions, and improving what it learns. You’re giving your team a better memory and a master key to the universe.</p><h3>Workflow Patterns</h3><h4>Prompt Chaining</h4><p>Prompt chaining is one of those methods that will be the only sensible choice once you’ve begun to do it. You build little, discrete steps in combination rather than having your LLM try a gigantic task simultaneously. One by one, each brings the model closer to where you need it to be. It’s similar to solving a puzzle — piece by piece.</p><p>For instance, if you’re creating an agent to assist customers in planning a holiday, rather than providing the model with a long prompt like “<em>Plan a 5-day trip to Japan including flights, accommodations, and activities,</em>” you do it step-by-step. Then, first, you’d type, “<em>What are the best places to visit in Japan?</em>” After getting that list, you would type, “<em>What are the best flights to Tokyo?</em>” Third, “<em>Find hotels in Tokyo for 3 nights,</em>” and last, “<em>Give me a day-by-day itinerary in Tokyo.</em>”</p><p>This strategy is not just more straightforward for the model but for you as well. You gain better control of the process by breaking down the job into smaller portions. If you have made some mistakes, you can re-tune the follow-up prompt or go back and fix the last step. It is like talking, wherein you can set the tone and direction while carrying on.</p><p>The second benefit is <strong>flexibility</strong>. Add, remove, and resequence steps with prompt chaining are simple. You can insert a budgeting step or rethink and adjust a prior response. It is a flexible process that adapts to your requirements.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85e32898e092bb1cb313993d8006c4e9/href">https://medium.com/media/85e32898e092bb1cb313993d8006c4e9/href</a></iframe><p>It’s a simple, natural method of tackling complex tasks, and it is the way we naturally get things done — step by step, with space to adjust in the process.</p><h4>Gating</h4><p>When creating an AI agent, becoming enthusiastic about what it can accomplish is easy — responding to questions, extracting data, and getting things done. But even more important is how it understands what to do and when. That’s where gating comes into play. It’s creating decision points or checkpoints within your agent’s workflow to stay on track and not fritter time away or go down the wrong path.</p><p>Imagine this: your agent in a maze. It would push through without gating, taking blind turns and wishing it well. With gating, it stops at the crossroads, looks over, determines if it will proceed, circles, or closes up shop. It’s easy to insert smarts and productivity into your agent’s trip.</p><p>For instance, in the case of a customer support agent. If asked, the agent would first determine if the question is simple. If not, it would ask for additional details first before answering. Or, if it is requesting a database and doesn’t get anything, it might crash and inform you rather than going on without having all the facts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae7b5d32acdfc1bb5bfc20377085a296/href">https://medium.com/media/ae7b5d32acdfc1bb5bfc20377085a296/href</a></iframe><p>Gating is helpful in complex workflows where a single mistake could sabotage the whole job. Picture an invoice-processing agent. The agent can gate on each step — such as error checking or missing fields — like extracting, validating, and passing it on to accounting. If there is something amiss, it gates and alerts the problem rather than going ahead and causing a mess.</p><p>Gates may be as simple as a pair of conditional checks or as complex as a decision module. Creating such gates where necessary is necessary so your agent becomes intelligent, not conscientious. Consider how it will make decisions rather than merely considering what it will do in creating an AI agent. Gating is an easy yet effective way of keeping your agent in line and always moving towards the correct goal.</p><h4>Routing</h4><p>Okay, now routing — <em>because even AI agents require a little guidance in life.</em> Routing is the GPS of the system, ever quietly steering tasks, queries, and information to the correct location at the right time. It is not flashy but prevents your agent from getting stuck in the weeds.</p><p>When a task comes in, your agent doesn’t just throw darts at a board blindly to determine what to do. It pauses (figuratively) and determines: “<em>Do I make an API call? Send this to another agent? Or process it myself?</em>” Routing figures out those choices, sending each task to where it belongs.</p><p>For example, if you’re making a customer support representative, a user asks, “<em>What’s my account balance?</em>” Routing answers, “<em>Billing API, you’re up.</em>” But if the user asks, “<em>What’s a decent noodle recipe?</em>” it might send that to a recipe database or get the LLM to do it in-house. It’s all about finding and putting the task into the right tool or path.</p><p>What the routing is so thrilling about is how flexible it is. Your agent is not bound to the script repeatedly and can turn on a dime. One minute, the agent is fetching data; the next minute, it’s generating a report; and the next minute, it’s generating an email. Routing helps your agent understand where to send each task, no matter how random they are.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb393b62e3f0b20411e57a2ebf5d75b3/href">https://medium.com/media/eb393b62e3f0b20411e57a2ebf5d75b3/href</a></iframe><p>And the good news? Routing doesn’t have to be complicated. It can be as simple as some “if-else” statements, LLM calls, or as evolved as a machine learning model that determines the best route based on past behaviour. The intention is to make it flexible so your agent can do whatever you throw. While routing isn’t the show’s star, the behind-the-scenes hero is what makes it all happen. It’s the decision-maker, the problem-solver, the glue that keeps your agent together. And to be honest, that’s what makes it so darn crucial.</p><h4>Parallelization: Sectioning</h4><p>Sectioning is excellent <em>if the subtasks you break a task into can run in parallel</em>, which provides your AI agent with a good speedup. The idea is simple: split a giant task into teeny-tiny, separate pieces and have your agent work through them all at once. It’s similar to having many experts work together, each doing his portion of the job.</p><p>LLMs work better if each subtask has its very own call. Instead of making one LLM deal with many considerations, you allow each call to focus on one aspect. This improves precision and makes the whole process quicker and more effective.</p><p>For example, suppose you’re creating a system where one instance of an LLM handles end-user questions while another does guardrails for offensive content or requests. It’s better to have it this way than to have the guardrails plus the core answer done by a single LLM call. By distributing the work between the two of them, you enable each LLM to utilize its best strengths while you get quality results overall.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e61e6d58bd84d9bb9082b14e4d70f42d/href">https://medium.com/media/e61e6d58bd84d9bb9082b14e4d70f42d/href</a></iframe><p>Another great example is measuring LLM performance. Instead of taking one LLM call attempt to measure everything simultaneously, several calls can test different aspects of the model performance for a given prompt. One call can test for facts, another for tone, and another for relevance. Testing these in parallel speeds up the process and enables you to receive more granular and actionable feedback.</p><p>Sectioning is not merely splitting work into smaller pieces — it’s about making them work in parallel to deliver the best possible efficiency. It’s a speed, accuracy, and performance game-changer when done right.</p><h4>Parallelization: Voting</h4><p>Many times, one go-through of an activity isn’t enough. That’s where voting comes in. It’s simply repeating the same task repeatedly to get different outputs or perspectives, especially when you need more confidence in the result. Consider asking experts for their opinions — each has a different perspective, giving you a more trusted answer.</p><p>Voting is helpful for nuanced problems with many factors, such as juggling accuracy and danger. Say you’re analyzing some code for security vulnerabilities. Instead of using a single call to one LLM, you could make a series of calls on individual prompts to work their way through the code, each checking a specific vulnerability. If only one returns a problem, you know it’s worth pursuing further.</p><p>Another excellent use case is to decide whether the content is inappropriate. You could have several prompts that judge various things — tone, language, or context — and different vote thresholds to trade off false positives and negatives. One prompt may look for offensive language, another for sensitive topics, and a third for general context. By averaging their results, you have a more nuanced and precise judgment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6a08240feb8d9831abd8493531d2827/href">https://medium.com/media/a6a08240feb8d9831abd8493531d2827/href</a></iframe><p>Voting is extremely powerful since it adds depth and predictability to your AI agent’s decision-making. You’re not rooted in a single response, but gathering multiple, fewer mistakes, more confidence in outcomes. It’s like seeking the advice of several friends over one tough decision- each of which contributes something uniquely different, altogether allowing them to give you a broader picture. Use voting to get diverse outputs, compare them, and make smarter, more informed decisions. It’s a low-key idea but a giant leap toward developing an agent you can trust. And come on — a little confidence goes a long way regarding AI(concept of deep seek?).</p><h4>Orchestrator-Workers</h4><p>Consider it: you’ve got a cluttered project and have no idea where to begin. That’s what the <strong>orchestrator worker</strong>’s workflow is for. You’ve got this project manager (the orchestrator) who takes the chaos, breaks it into smaller, manageable pieces, and subcontracts it to a team of specialists (the workers). With them, you’ve got the project done — economically and quickly.</p><p>That is where the orchestrator LLM gets it done. It has no agenda; it makes decisions along the way. It gets the job, determines how to break it down, and sends each piece to an LLM worker. The workers start, and the orchestrator assembles them into one completed product.</p><p>For instance, if you’re creating a tool that has to perform complicated editing on code, The orchestrator decomposes the work, identifies what files must be changed and assigns each worker. The workers perform their tasks, and the orchestrator constructs the changes.</p><p>A search task requires getting and reviewing information from multiple sources. The orchestrator may discover the best sources and allocate each to a worker to question and summarize the results into an easy solution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd58f805de0cbcb50002aeb9cfe89f8d/href">https://medium.com/media/dd58f805de0cbcb50002aeb9cfe89f8d/href</a></iframe><p>What is excellent about this workflow is that it is highly flexible. Unlike parallelization, where the jobs are pre-specified and run parallel, the orchestrator-workers model can learn to adapt to the job. The brain and muscles cooperate to complete the job well, wisely, and smoothly.</p><h4><strong>Evaluator-Optimizer</strong></h4><p>At times, things <em>being done</em> are not sufficient. You want them <strong><em>done well</em></strong>. This workflow is about reviewing what your AI agent gives you, going through it, and making it better so it’s the best it can be. It’s as if having an editor tell you what’s not good and assisting you in making it better.</p><p>That’s how it works: Initially, the evaluator leaps in. It checks for output, sees errors, and sees how much the output has adhered to the task standards. They’re like the quality control department, giving everything a shape. When the evaluator has gone through, the optimizer steps in. As per the evaluation from the evaluator, it alters, sharpens, and fine-tunes the output. Between them, they turn good outcomes into great ones.</p><p>For example, if you are developing an AI agent to compose marketing copy, the evaluator would read the text for tone, clarity, and relevance. At the same time, the optimizer would shape the language into something more compelling or persuasive. Or if you are developing a coding assistant. The evaluator could highlight potential bugs or inefficiencies, and the optimizer would clean up and optimise the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4b62ec83def82647a8aef9a8caacf11/href">https://medium.com/media/f4b62ec83def82647a8aef9a8caacf11/href</a></iframe><p>Instead of taking the first draft, you’re iterating and refining until the output is as good as possible. You have a feedback loop in the system, so your agent improves over time. Building an AI agent where quality matters — writing, coding, or anything in between — the evaluator-optimizer process is your approach. <strong><em>It’s not about getting the job done; it’s about doing it well.</em></strong></p><p>We are at the end of a wild ride through the <strong>API-first AI agentic patterns world</strong>. We’ve moved from <strong><em>prompt chaining</em></strong> and <strong><em>parallelization</em></strong> to <strong><em>orchestrator</em>-<em>workers</em></strong><em> </em>and <strong><em>evaluator</em>-<em>optimizers</em></strong>. And, of course, <strong><em>routing</em></strong>, <strong><em>sectioning</em></strong>, and <strong><em>voting </em></strong>— because who doesn’t love some multitasking AI?</p><p>The bad news? You don’t need a full-fledged framework to do any of this. With direct APIs, some creative imagination, and maybe a lot of coffee, you can write AI agents as smart, fast, and friendly as you ever imagined. If you’re automating support, handling information, or building the following considerable coding utility, these patterns give you the tools to make it happen.</p><p>I’ve included a few code samples <em>(thanks to OpenAI SDK and Node.js devs!)</em> to kick-start you, but the <strong>real trick is extending these ideas into your projects</strong>. The patterns that the Anthropic authors and I have outlined are akin to LEGO pieces — build combinations, customize, and make your own.</p><p>Before we leave you, thank the <a href="https://www.anthropic.com/engineering/building-effective-agents">Anthropic blog and team</a> for the inspiration. Their ideas regarding AI agentic patterns were the spark that ignited this whole operation. If you haven’t, look at their stuff — it’s a goldmine of thought.</p><p>So, now what? Your call. Take these patterns, flip on your IDE, and go code. Whether you’re an old pro at coding or just starting to dip your toes into AI — there’s never a worse time to try, improve, and make something amazing.</p><p>And hey, if you do something amazing, share it with the world. Because the best part of this ride isn’t even what you create — you’ll be encouraging other folks to develop, too. So create. The future of AI agents is yours to shape — and for real, it will be awesome.</p><h4>References</h4><ul><li><a href="https://www.anthropic.com/engineering/building-effective-agents">Building Effective Agents — <em>Erik Schluntz and Barry Zhang, Anthropic</em></a></li><li><a href="https://github.com/anthropics/anthropic-cookbook/tree/main/patterns/agents">Building Effective Agents Codebook — <em>Anthropic</em></a></li><li><a href="https://platform.openai.com/docs/overview">OpenAI SDK Docs — <em>OpenAI</em></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a0bac2f1b04" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/api-first-ai-agentic-patterns-building-smarter-systems-without-the-framework-overhead-1a0bac2f1b04">API-First AI Agentic Patterns: Building Smarter Systems Without the Framework Overhead</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Guide to Modular Design with Web Sockets in Node.js]]></title>
            <link>https://levelup.gitconnected.com/a-guide-to-modular-design-with-web-sockets-in-node-js-86441cfdb59d?source=rss-d71bb8057a4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/86441cfdb59d</guid>
            <category><![CDATA[sockets]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[websocket]]></category>
            <dc:creator><![CDATA[Ujjwal Jindal]]></dc:creator>
            <pubDate>Tue, 26 Dec 2023 16:17:35 GMT</pubDate>
            <atom:updated>2023-12-26T16:17:35.686Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UK1IfHzoPDNrJKaN4VDS0w.png" /><figcaption>Web Sockets by <a href="https://www.linkedin.com/in/jindalujjwal0720/">Ujjwal Jindal</a> on Canva</figcaption></figure><p>In the dynamic landscape of web development, harnessing the power of real-time communication is pivotal. Web sockets have emerged as a critical technology, enabling seamless, bidirectional data flow between clients and servers. Coupled with modular design principles, the potential for building robust and scalable applications becomes even more apparent.</p><p>This blog explores the synergy between modular design and web sockets in the context of Node.js. Assuming a foundational understanding of web sockets and modular architecture, we will delve into practical strategies for structuring your Node.js applications. By the end of this journey, you’ll be equipped with insights into creating modular, maintainable, and scalable applications that leverage the capabilities of web sockets for real-time communication.</p><p>Let’s embark on this exploration, where we’ll discuss the theoretical underpinnings and dive into hands-on examples and best practices. Whether you’re a seasoned developer or someone stepping into the realm of real-time applications, this guide will provide valuable insights into architecting Web Sockets in Node.js applications for optimal performance and maintainability.</p><h4>Tangled Threads with Non-Modular Web Sockets</h4><p><strong>Monolithic Structure: <br></strong>In non-modular WebSocket code, a common pitfall is adopting a monolithic structure where all WebSocket functionality is confined to a single file or module. This approach can lead to code that is hard to maintain and scale. For instance, envision a chat application(remember those YouTubers?) where all WebSocket handling logic resides in a single file.</p><pre>// Monolithic Structure<br>// All WebSocket logic confined to a single file<br>const { WebSocketServer } = require(&#39;ws&#39;);<br>const wss = new WebSocketServer({ /* server options */ });<br><br>wss.on(&#39;connection&#39;, (ws) =&gt; {<br>    // Handle all WebSocket events in this file<br>    ws.on(&#39;message&#39;, handleMessage);<br>    ws.on(&#39;close&#39;, handleClose);<br>});</pre><p><strong>Hard-to-Follow Event Handling: <br></strong>In non-modular code, managing WebSocket events can be more precise. As the application grows, tracking how different events are handled becomes challenging. Visualise a scenario where various events are scattered throughout the codebase without a clear structure.</p><pre>// Event Handling Chaos<br>// WebSocket events scattered across the codebase<br>const { WebSocketServer } = require(&#39;ws&#39;);<br>const wss = new WebSocketServer({ /* server options */ });<br><br>wss.on(&#39;connection&#39;, (ws) =&gt; {<br>    // Handle connection event<br>    // ...<br><br>    ws.on(&#39;message&#39;, (message) =&gt; {<br>        // Handle message event<br>        // ...<br>    });<br><br>    // More events...<br>});</pre><p>This way, the file gets bigger and bigger with time, and all the logic related to sockets mix up. This architecture is suitable for small projects but not large codebases where multiple services require sockets to communicate with the clients.</p><p><strong>Limited Reusability: <br></strong>Non-modular WebSocket code often needs more reusability, making integrating WebSocket functionality into different application parts challenging. Consider a situation where a specific WebSocket feature needs to be replicated across multiple modules. Direct callbacks to web sockets are not much use as they are tightly coupled with the sockets.</p><p><strong>Debugging Nightmares: <br></strong>Debugging non-modular WebSocket code can be daunting, especially when issues arise. With WebSocket logic scattered throughout the application, pinpointing the source of a problem becomes challenging. Let’s say you have a code written with a non-modular approach of web sockets (as followed in many tutorials on the web); if you encounter some bug or issue with the logic, it becomes hard to pinpoint and debug the problem.</p><p>As we uncover these pitfalls, the subsequent sections of this guide will offer practical solutions and demonstrate the advantages of adopting a modular approach when working with WebSockets in Node.js. Let’s untangle the threads and embrace a more organised, scalable, and maintainable WebSocket architecture.</p><h4>Solution: Modular WebSocket Handling with SocketRouter</h4><p>I have written a SocketRouter class to solve some of the above problems. The SocketRouter class introduces a modular and organised paradigm for handling WebSocket messages in Node.js applications.</p><pre>// router.js<br>/**<br> * @type HandlerFunction<br> * @param {any} payload<br> * @param {WebSocket} ws<br> * @param {WebSocketServer} wss<br> * @returns {void}<br> */<br><br>/**<br> * SocketRouter<br> * @class<br> */<br>class SocketRouter {<br>  constructor() {<br>    /** @type string */<br>    this._type = null;<br>    /** @type Record&lt;string, HandlerFunction&gt; */<br>    this._types = {};<br>  }<br><br>  /**<br>   * Add sub-router<br>   * @param {string} type<br>   * @param {SocketRouter} router<br>   * @returns {void}<br>   */<br>  use = (type, router) =&gt; {<br>    const handlers = router._getHandlers();<br>    for (const [key, handler] of Object.entries(handlers)) {<br>      const newType = `${type}:${key}`;<br>      this._types[newType] = handler;<br>    }<br>  };<br><br>  /**<br>   * Register a new type of message<br>   * @param {string} type<br>   * @param {HandlerFunction} handler<br>   * @returns {void}<br>   */<br>  type = (type, handler) =&gt; {<br>    this._types[type] = handler;<br>  };<br><br>  /**<br>   * Handle a message<br>   * @param {string} type<br>   * @param {any} payload<br>   * @param {WebSocket} ws<br>   * @param {WebSocketServer} wss<br>   */<br>  handle = (type, payload, ws, wss) =&gt; {<br>    /** @type HandlerFunction */<br>    const handler = this._types[type];<br>    if (handler) {<br>      handler(payload, ws, wss);<br>    } else {<br>      ws.send(JSON.stringify([&quot;error&quot;, `No type found for ${type}`]));<br>    }<br>  };<br><br>  _getHandlers = () =&gt; {<br>    return this._types;<br>  };<br>}<br><br>module.exports = SocketRouter;</pre><p>Let’s delve into the key features and benefits it brings to the table:</p><ol><li><strong>Structured Modularity: <br></strong>At the heart of the SocketRouter class is the ability to organise WebSocket message handling in a structured, modular manner. Developers can register distinct message types, and their corresponding handler functions through the type method, fostering a clear separation of concerns.</li><li><strong>Sub-Routers for Hierarchical Organisation:</strong><br>The use method empowers developers to create sub-routers, allowing for a hierarchical organisation of WebSocket message handling. This is particularly useful for breaking down complex applications into manageable and isolated modules, each with its message types and handlers.</li><li><strong>Global State Isolation:</strong><br>Global state isolation is achieved by encapsulating message types and handlers within class instances. This prevents unintended naming conflicts and promotes a clean, isolated environment for each router instance.</li><li><strong>Dynamic Registration of Handlers:</strong><br>The SocketRouter class enables dynamic registration of message types and handlers, fostering flexibility and reusability. This is especially valuable when extending or customising WebSocket functionality for different modules or features within an application.</li><li><strong>Centralised Message Handling:</strong><br>The handle method serves as a centralised point for WebSocket message handling. This simplifies the logic for handling messages and aids debugging by providing a single entry point to trace the message processing flow.</li><li><strong>Encouraging Scalability and Maintainability:</strong><br>The SocketRouter class lays the groundwork for scalable and maintainable WebSocket applications through its modular design. Whether dealing with a simple chat module or a complex gaming system, this class accommodates diverse requirements while maintaining a clean and organised codebase.</li></ol><p>Incorporating the SocketRouter class into your Node.js WebSocket projects can lead to a more organised, scalable, and maintainable architecture. As we explore further in the implementation, you’ll witness how this class seamlessly integrates with your WebSocket server, unlocking the full potential of modular design in real-time communication applications. Let’s continue the journey into practical implementation and discover the elegance of modular WebSocket handling with the SocketRouter class.</p><p>The index.js file where your sockets will be initialised can be configured the following way:</p><pre>// index.js<br>const { WebSocketServer } = require(&#39;ws&#39;);<br>const wss = new WebSocketServer({ /* server options */ });<br><br>// the main socket router for the app<br>const router = require(&quot;./routes&quot;);<br><br>wss.on(&quot;connection&quot;, (socket) =&gt; {<br>  // socket error<br>  socket.on(&quot;error&quot;, (err) =&gt; {<br>    console.error(&quot;Web Socket error:&quot;, err);<br>  });<br><br>  // socket close<br>  socket.on(&quot;close&quot;, () =&gt; {<br>    // no action for now<br>  });<br><br>  // socket message<br>  socket.on(&quot;message&quot;, function (message) {<br>    try {<br>      try {<br>        var [type, payload] = JSON.parse(message);<br>      } catch (err) {<br>        throw new Error(&quot;Invalid message format&quot;);<br>      }<br>      router.handle(type, payload, socket, wss);<br>    } catch (err) {<br>      socket.send(JSON.stringify([&quot;error&quot;, err.message]));<br>    }<br>  });<br>});</pre><h4>Some Example Use Cases: Leveraging SocketRouter in Various Applications</h4><p><strong>Real-Time Chat Application:</strong></p><p>In a real-time chat application, different message types may include <em>message</em>, <em>userJoin</em>, and <em>userLeave</em>. The SocketRouter class facilitates the modular handling of these messages, allowing for easy integration and extension.</p><pre>const SocketRouter = require(&quot;./router&quot;)<br>const router = new SocketRouter();<br><br>router.type(&#39;message&#39;, handleMessage);<br>router.type(&#39;userJoin&#39;, handleUserJoin);<br>router.type(&#39;userLeave&#39;, handleUserLeave);<br><br>module.exports = router;</pre><pre>// index router<br>const SocketRouter = require(&quot;./router&quot;)<br>const router = new SocketRouter();<br><br>router.use(&#39;chats&#39;, require(&quot;./chat-router&quot;));<br>// ... other routers<br><br>module.exports = router;</pre><p><strong>Online Multiplayer Game:</strong></p><p>For an online multiplayer game where real-time communication is crucial, the SocketRouter class can manage messages like <em>playerMove</em>, <em>attack</em>, and <em>gameOver</em>. Sub-routers can further organize logic for specific game features.</p><pre>// player router<br>const SocketRouter = require(&quot;./router&quot;)<br>const router = new SocketRouter();<br><br>router.type(&#39;playerMove&#39;, handlePlayerMove);<br>router.type(&#39;attack&#39;, handleAttack);<br><br>module.exports = router;</pre><pre>// game router<br>const SocketRouter = require(&quot;./router&quot;)<br>const router = new SocketRouter();<br><br>router.type(&#39;gameOver&#39;, handleGameOver);<br><br>module.exports = router;</pre><pre>// index router<br>const SocketRouter = require(&quot;./router&quot;)<br>const router = new SocketRouter();<br><br>router.use(&#39;players&#39;, require(&quot;./player-router&quot;));<br>router.use(&#39;games&#39;, require(&quot;./game-router&quot;));<br><br>module.exports = router;</pre><p>The above example shows us how the SocketRouter can be used</p><p><strong>Collaborative Document Editing:</strong></p><p>Message types in a collaborative document editing application include <em>textUpdate</em>, <em>cursorMove</em>, and <em>userPresence</em>. The SocketRouter class allows for the efficient handling of these collaborative events.</p><pre>// document router<br>const SocketRouter = require(&quot;./router&quot;)<br>const router = new SocketRouter();<br><br>router.type(&#39;textUpdate&#39;, handleTextUpdate);<br>router.type(&#39;cursorMove&#39;, handleCursorMove);<br>router.type(&#39;userPresence&#39;, handleUserPresence);<br><br>module.exports = router;</pre><h4>Conclusion:</h4><p>In Node.js WebSocket applications, SocketRouter can emerge as a game-changer. 🚀 Its modular design and hierarchical organization empower developers to architect scalable and maintainable real-time solutions. With SocketRouter, handling diverse message types becomes a seamless experience, promoting clarity and extensibility.</p><p>In your Node.js journey, let SocketRouter be your ally in crafting responsive and organized real-time applications. Elevate your projects with the simplicity and efficiency SocketRouter brings to WebSocket communication. 🌐✨</p><p>The code snippets are taken from <a href="https://github.com/we-festify/festify">Festify</a> — The best college/university fests and events management framework.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86441cfdb59d" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/a-guide-to-modular-design-with-web-sockets-in-node-js-86441cfdb59d">A Guide to Modular Design with Web Sockets in Node.js</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Web-Push Notifications in React and Node: One-Stop Guide]]></title>
            <link>https://blog.stackademic.com/mastering-web-push-notifications-in-react-and-node-one-stop-guide-15de9567aa9a?source=rss-d71bb8057a4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/15de9567aa9a</guid>
            <category><![CDATA[notifications]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Ujjwal Jindal]]></dc:creator>
            <pubDate>Wed, 06 Dec 2023 08:17:15 GMT</pubDate>
            <atom:updated>2023-12-11T03:04:45.008Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8gS06sZ5eLVQq6Kp" /><figcaption>Photo by <a href="https://unsplash.com/@lunarts?utm_source=medium&amp;utm_medium=referral">Volodymyr Hryshchenko</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Greetings! 🚀 Ever needed to spice up your app&#39;s user engagement? Well, you&#39;re in for a treat! Today, we&#39;re delving into the realm of web-push notifications in React and Node, and guess what? It&#39;s smoother than a hot knife through butter.</p><p>I&#39;ve read many articles and docs and watched numerous tutorials, but I have yet to find a complete solution to this problem. I am writing an article on the complete solution from A to Z. After reading this article; you won’t need any support configuring web-push notifications in your React and node app.</p><p>There is no coding acrobatics; it is just a simple guide to seamlessly integrating web-push notifications into your React project. So, grab your coding gear, and let&#39;s embark on this journey to elevate your app&#39;s communication game without breaking a sweat. Ready? Let&#39;s dive in!</p><h3>Configuring the Service Worker properly</h3><p>You can safely ignore this part if you have configured your React app to handle the registration of service workers. I have searched a lot of issues and answers for creating service workers in the React app, but I need a part to explain it easily to beginners.</p><h4>Service Worker</h4><p>Before we dive into the exciting world of web push notifications, let&#39;s chat about the unsung hero behind the scenes — the Service Worker. Think of it as the backstage crew making the magic happen. This tiny script runs in the background, handling tasks like caching, push notifications, and more, making your React app a powerhouse of user delight.</p><p>If you are starting a new project, using the PWA template provided by React would be best. Use the following command to create, and you&#39;re done.</p><pre>npx create-react-app my-app --template cra-template-pwa</pre><p>However, web-push is a service developers think of later in their projects and find it challenging to migrate from the standard React app to PWA. For this, you can follow these steps to relocate quickly.</p><h4>Steps to Properly Install Service Worker in React App</h4><ol><li>Create a <em>manifest.json </em>file in the <em>public</em> folder and copy the contents from the <a href="https://github.com/cra-template/pwa/blob/main/packages/cra-template-pwa/template/public/manifest.json">official template</a>. Customise this according to your app details.</li><li>Copy the <a href="https://github.com/cra-template/pwa/blob/main/packages/cra-template-pwa/template/src/service-worker.js">service-worker.js</a> file and the <a href="https://github.com/cra-template/pwa/blob/main/packages/cra-template-pwa/template/src/serviceWorkerRegistration.js">serviceWorkerRegistration.js</a> file from the provided links into the <em>src</em> folder of your React app.</li><li>Copy all the dependencies starting with &quot;<em>workbox-</em>&quot; from <a href="https://github.com/cra-template/pwa/blob/main/packages/cra-template-pwa/template.json">template.json</a> to your <em>package.json </em>and run<em> &quot;npm i&quot; </em>to install the dependencies. Versions can be changed later on based on requirements.</li><li>Register the service worker in your index.js file, and you&#39;re done🎉</li></ol><h4>Note —</h4><p>On running the app with &quot;<em>npm start</em>&quot;, the app runs in debug mode. Service Workers are only registered over HTTPS. Therefore, you won&#39;t see any service worker registration in the <em>application</em> tab of dev tools.</p><p>You have to use &quot;<em>npm run build</em>&quot; and then serve that to see the service worker registration. Do we then need to build every time we change the service worker script to see the changes? Sadly, yes. But there is a workaround to this. After facing this issue a lot, I tried ways in which this was satisfactory. (Ensure you don&#39;t use the cache first strategy with this approach for testing)</p><p>The solution is to create a blank <em>service-worker-dev.js </em>file directly in the public folder of the React app. Update the code in the <em>serviceWorkerRegistration.js</em> to support our dev service worker.</p><pre>// serviceWorkerRegistration.js<br>function nodeEnvProductionCheck(config) {<br>  if (config &amp;&amp; config.bypassNodeEnvProduction) {<br>    return config.bypassNodeEnvProduction;<br>  }<br>  return process.env.NODE_ENV === &quot;production&quot;;<br>}<br><br>function getServiceWorkerUrl() {<br>  if (process.env.NODE_ENV === &quot;production&quot;) {<br>    return `${process.env.PUBLIC_URL}/service-worker.js`;<br>  }<br>  return `/service-worker-dev.js`;<br>}<br><br>export function register(config) {<br>  if (nodeEnvProductionCheck(config) &amp;&amp; &quot;serviceWorker&quot; in navigator) {<br>      // ...<br><br>      window.addEventListener(&quot;load&quot;, () =&gt; {<br>        const swUrl = getServiceWorkerUrl();<br>        // ...<br><br>      }<br>  }<br>}</pre><p>A slight modification to the calling of the register function in index.js file will make it work smoothly after that.</p><pre>serviceWorkerRegistration.register({<br>  bypassNodeEnvProduction: true,<br>});</pre><h3>Push Notifications</h3><p>Push notifications are bite-sized alerts on a user&#39;s device, even when they&#39;re not actively using your app. They serve as attention-grabbing nudges, ensuring your users stay in the loop with the latest and greatest from your application.</p><h4>Playing with the Notification API</h4><p>Let&#39;s not include the server for now and understand only the client part, i.e. the browser&#39;s native Notification API. To show notifications to the user, we need to request permission from the user first. Let&#39;s create a new service inside our <em>src </em>for handling notifications. This code is not inside the service worker, and the service worker is not required for this part.</p><pre>// notification.js<br>export const requestPermission = async () =&gt; {<br>  if (!(&quot;Notification&quot; in window)) {<br>    throw new Error(&quot;Notification not supported&quot;);<br>  }<br>  const permission = await window.Notification.requestPermission();<br>  if (permission !== &quot;granted&quot;) {<br>    throw new Error(&quot;Permission not granted for Notification&quot;);<br>  }<br>};<br><br>export const showNotification = async (title, options) =&gt; {<br>  if (!(&quot;Notification&quot; in window)) {<br>    throw new Error(&quot;Notification not supported&quot;);<br>  }<br>  if (window.Notification.permission !== &quot;granted&quot;) {<br>    throw new Error(&quot;Permission not granted for Notification&quot;);<br>  }<br>  return window.Notification(title, options);<br>};</pre><p>The code above has two functions. The first function checks for the native Notification API in the window, and if it is present, request permission from the user. The later function checks for the presence of Notification API and the permission and then shows the notification. Let&#39;s try to use them and see how they work.</p><pre>// App.jsx<br>import React from &quot;react&quot;;<br>import &quot;./App.css&quot;;<br>import { requestPermission, showNotification } from &quot;./notification&quot;;<br><br>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;div id=&quot;notification-container&quot;&gt;<br>        &lt;button<br>          id=&quot;notification-permission-button&quot;<br>          onClick={() =&gt; requestPermission()}<br>        &gt;<br>          Enable Notifications<br>        &lt;/button&gt;<br>        &lt;button<br>          id=&quot;show-notification-button&quot;<br>          onClick={() =&gt; showNotification(&quot;Yes, it worked!&quot;)}<br>        &gt;<br>          Show Notification<br>        &lt;/button&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>The above code is a straightforward app with only two buttons, one for requesting notification permissions and the other for showing a notification. When you run the above app and click the request permission button, you will see a popup from the browser asking you for permission to show notifications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*a07mZGF9woEopAtIjxQwLw.png" /><figcaption>Browser Request notification popup</figcaption></figure><p>After the user has permitted the app to show notifications, you can click the show notification button and see a native notification on your screen somewhat like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/469/1*XM-A-XYa46FRck86S5nv9w.png" /></figure><p>Good job if you&#39;ve made it so far. These are the basics of how browser notifications work and how to use the native Notification API to handle notifications. A good piece of advice is to only request notification permission from the user after some actual need. For example, if the user orders something, you can ask for his permission to show the order live details. This way, there will be more chances to allow permission than to deny.</p><p>But now, you will say these are not &#39;notifications&#39; yet. The notifications we see from the apps daily pop up automatically; we don&#39;t need to click a button to show notifications. The next part of the blog will focus on this.</p><h4>How do push notifications work?</h4><p>I want to avoid going deep into the theory, but you will only understand the implementation with this. I&#39;ll make sure I make you understand effortlessly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*Ht1spjyfTTn0vh-APPW7tA.png" /><figcaption>Web Push Flow created by <a href="https://www.linkedin.com/in/jindalujjwal0720/">Ujjwal Jindal</a> on <a href="http://draw.io/">draw.io</a></figcaption></figure><p>I have broken down the whole flow into four categories:</p><ul><li>Blue — We make these calls to the browser or our server.</li><li>Grey — These are automated calls handled by different services and 3rd party providers.</li><li>Dotted — These are the responses from the browser to our React app.</li><li>Black — These are the actions we have to take from the server.</li></ul><p>To understand the above diagram and the flow of push notifications, this coloured diagram is essential. Most of the articles I read made the diagram monochromatic, which didn&#39;t help the developers understand their part and what is done automatically.</p><p>Let&#39;s move on to understanding them individually. We have already covered the first phase of this diagram above, i.e., the service worker registration. After we are sure the service worker is registered, we can continue subscribing to push notifications. For push notifications, every major browser has its 3rd party servers for handling the &quot;<em>push</em>&quot; event. Subscribing to push notifications is asking for a unique ticket by which you can identify the individual user. The subscription has a unique endpoint which refers to a unique user in the 3rd party database. Whenever we have to notify the user, we will hit this endpoint with a notification, and the 3rd party push service will do the rest of the work under the hood.</p><p>So, in a nutshell, we only need to do the following things broadly to set up a whole notification system for our app:</p><ol><li>Register a service worker (already done above)</li><li>Ask for notification endpoint (subscription)</li><li>Store the subscription with user details on our server</li><li>Send a notification to the user</li><li>Listen for notification</li></ol><h3>Implementation</h3><p>This part is crucial for all the developers as all the other articles on the internet focus on implementing in vanilla JS, making it hard to use and understand the implementation in the React environment. Let&#39;s write code for the web-push service, and I&#39;ll explain it after the code.</p><pre>// client/services/webPush<br>class WebPushService {<br>  static hasPermission() {<br>    return Notification.permission === &quot;granted&quot;;<br>  }<br><br>  static async requestPermission() {<br>    return await Notification.requestPermission();<br>  }<br><br>  static async getSubscription() {<br>    return await navigator.serviceWorker.ready.then(async (registration) =&gt; {<br>      return await registration.pushManager.getSubscription();<br>    });<br>  }<br><br>  static async subscribe() {<br>    const registration = await navigator.serviceWorker.ready;<br>    const subscription = await registration.pushManager.subscribe({<br>      userVisibleOnly: true,<br>      applicationServerKey: process.env.REACT_APP_VAPID_PUBLIC_KEY,<br>    });<br>    return subscription;<br>  }<br><br>  static async unsubscribe() {<br>    const subscription = await this.getSubscription();<br>    if (subscription) {<br>      await subscription.unsubscribe();<br>    }<br>    return subscription;<br>  }<br>}<br><br>export default WebPushService;</pre><p>The above code is self-explanatory for a good developer, but I&#39;ll review it individually. The class <em>WebPushService</em> is for enclosing the business logic for web push. It has five main functions, which can be combined based on the requirements.</p><ul><li>hasPermission() — checks if the user has already provided permission for notifications to the app.</li><li>requestPermission() — requests the notification permission from the user. If the user has already given the consent, this function will return the permission directly instead of showing the popup.</li><li>getSubscription() — it first waits until the service worker is ready, then requests the pushManager(3rd party) to get us the subscription if it&#39;s there. If the user has not subscribed to the push notifications, this will return null.</li><li>subscribe() — it first gets the registration object from the service worker, then requests the pushManager to subscribe this user agent(browser) to the push notifications and return us the subscription object.<br>But wait, have you asked what the arguments to that function mean? What are <em>userVisisbleOnly </em>and <em>applicationServerKey</em>?<br>The <strong><em>userVisibleOnly </em></strong>is a property that asks that all the notifications from this subscription always be shown to users and not for any internal app works. It is supposed to be an optional variable. Still, it is currently required to be &#39;<strong>true</strong>&#39; because the developers of this service aren&#39;t clear whether it should be used for any internal purposes.<br>The <strong><em>applicationServerKey </em></strong>is a public key that is uniquely identified by your server. This key is required because when you send notifications to the user via the push 3rd party, it will check the legitimacy of the notification by using your server&#39;s private key with this public key. If these keys match, the push will only be sent; otherwise, it will be ignored.</li><li>unsubscribe() — it just gets the subscription and tries to unsubscribe if it exists. It also returns the subscription to perform any side effects on your application server if required.</li></ul><p>We&#39;ve also now completed the second phase of our notifications setup, where we created a service to handle the push subscriptions. Storing it on the server is your task, as it can be done in various and unique ways depending on your requirements. However, I will provide an example usage of this service called on a toggle button for the notifications.</p><pre>// components/notifications<br>const NotificationSettingsGroup = () =&gt; {<br>  const [push, setPush] = useState(false);<br><br>  const handlePushChange = async (e) =&gt; {<br>    const { checked } = e.target;<br>    setPush(checked);<br>    try {<br>      if (!checked) {<br>        const payload = await WebPushService.unsubscribe();<br>        if (payload) {<br>          unsubscribeFromWebPush(payload); // server<br>        }<br>        return;<br>      }<br>      if (!WebPushService.hasPermission()) {<br>        await WebPushService.requestPermission();<br>      }<br>      let subscription = await WebPushService.getSubscription();<br>      if (!subscription) {<br>        subscription = await WebPushService.subscribe();<br>      }<br>      subscribeToWebPush(subscription); // server<br>      console.log(&quot;Subscribed to web push&quot;);<br>    } catch (error) {<br>      setPush(!checked);<br>      console.error(error);<br>    }<br>  };<br><br>  return (<br>    &lt;section&gt;<br>      &lt;h2&gt;Notifications&lt;/h2&gt;<br>      &lt;div&gt;<br>        &lt;p&gt;Web Push&lt;/p&gt;<br>        &lt;input type=&quot;checkbox&quot; checked={push} onChange={handlePushChange} /&gt;<br>      &lt;/div&gt;<br>    &lt;/section&gt;<br>  );<br>};</pre><p>In the above code, a React component is required to render and handle the subscriptions for web push. Please focus on the change handler function and its following the subscription path.</p><ul><li>First, we changed the UI by setting the <strong>push </strong>to a &#39;<em>checked</em>&#39; value to maintain the user experience.</li><li>If the user unchecked the checkbox, we have to remove the subscription, for which we can call our unsubscribe function from the service we created above. Note that we are also sending the payload(subscription that we returned from that function) to the server to unsubscribe the user from our server. We then return from this change handler as the task is finished.</li><li>If we reach ahead of that block, the user has checked the checkbox, pointing out that he wants the notifications. We then checked if the user had permitted us to show notifications; if not, we had to request permission from the user. After the user has allowed us to do so, we get its subscription object. If the subscription is absent, we subscribe to push by calling our services&#39; subscribe method and saving the subscription on our server.</li><li>If everything goes well, we return from the change handler; If not, or if any error is thrown while performing any of these tasks, we set the <strong>push </strong>again to its original value.</li></ul><p>Saving the subscriptions on the server is a daily task you can handle better than me. If you&#39;ve been following till now, you might have a working app with push notifications functionality. But hold on, it still needs to be completed. We have just gotten the details of the users&#39; subscriptions.</p><h4>How do you send Notifications?</h4><p>The push subscriptions we gathered from the users above contain an <em>endpoint. </em>Ideally, we would have to make a post-request to the endpoint without the authorisation header as our private key. We also need to debug any notifications that fail or the unpredictable errors that might happen. We will use a &quot; web-push &quot; package to let go of all these steps. This package is available in most of the languages you chose, but we will use the node js environment for ease of use.</p><pre>npm i web-push</pre><p>First, we must install and import the web-push package and create our VAPID keys. What are VAPID keys now? VAPID stands for <a href="https://datatracker.ietf.org/doc/rfc8292/">Voluntary Application Server Identification</a>, which uniquely identifies our server with a private and a public key. These keys should not be stored in a variable and pushed anywhere, as these are secret keys. To create vapid keys, write the following command in your terminal and copy the keys in your environment variables.</p><pre>npx web-push generate-vapid-keys</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vft8B4XUn4p9pQRibIdOSQ.png" /><figcaption>temp VAPID keys with subject</figcaption></figure><p>After generating the VAPID keys, we can implement the service to manage the notifications.</p><pre>// server/services/webPush<br>const webpush = require(&quot;web-push&quot;);<br><br>webpush.setVapidDetails(<br>  process.env.VAPID_SUBJECT,<br>  process.env.VAPID_PUBLIC_KEY,<br>  process.env.VAPID_PRIVATE_KEY<br>);<br><br>class WebPushService {<br>  static async sendNotificationToUser(<br>    user,<br>    notification,<br>    config = {<br>      topic: null,<br>      urgency: &quot;normal&quot;,<br>    }<br>  ) {<br>    try {<br>      if (!user) {<br>        throw new BadRequestError(&quot;User not found&quot;);<br>      }<br>      const subscription = await WebPushSubscriptionRepository.getByUser(<br>        user<br>      );<br>      if (!subscription) {<br>        throw new BadRequestError(&quot;User not subscribed to web push&quot;);<br>      }<br>      const payload = JSON.stringify({<br>        title: notification.title,<br>        body: notification.body,<br>        icon: notification.icon,<br>      });<br>      const options = {<br>        TTL: 60,<br>        topic: config.topic,<br>        urgency: config.urgency,<br>      };<br>      webpush.sendNotification(subscription, payload, options);<br>    } catch (err) {<br>      console.error(err);<br>    }<br>  }<br>}<br><br>module.exports = WebPushService;</pre><p>The function sendNotificationToUser() handles the sending of notifications to the user. You can customise the notification payload and options according to the specific needs of your application. The main thing in the above code is to set up the VAPID keys and then call the web-push <em>sendNotification </em>function to send the notification to the subscribed user&#39;s browser. Isn&#39;t that simple? Indeed. Ensure you give the VAPID_SUBJECT as it is where the associates may contact you in case of any problems or troubles with your push.</p><p>We are all done! But wait, I couldn’t see the notification in the browser when I sent it from the server🤔. Have I done anything wrong? Nope, you’ve followed it right.</p><h4>Listen for notifications</h4><p>We have completed the flow of our notifications but couldn&#39;t see the notifications pop up. Why? It is because the browser is getting the notification but doesn’t know what to do with it. The push service(3rd party) has sent the notification to the browser, and the browser sent it to the service worker by firing a “<strong>push</strong>” event. So, we must listen to that event to do something with the payload to show the notification. We can do it by the following lines of code:</p><pre>// service worker<br>    // ...<br><br>// listen for push event<br>self.addEventListener(&quot;push&quot;, (event) =&gt; {<br>  let { title, body, icon } = event.data.json();<br>  if (!title) title = &quot;New Notification&quot;;<br>  if (!body) return;<br>  self.registration.showNotification(title, {<br>    body,<br>    icon: icon || `${process.env.PUBLIC_URL}/logo192.png`,<br>  });<br>});</pre><p>The code is self-explanatory of what it is doing. It parses the notification in the JSON format and then shows it with the native Notification API we discussed above.</p><p>And we are now finally done. Cheers! Your app is now the ultimate chatterbox, keeping users in the loop and engaged. Your users will thank you, and your React app will never be the same again. Until next time, happy coding, and keep pushing those notifications like a boss! 🎉✨</p><h4>References</h4><ul><li><strong>Create React App</strong> (<a href="https://create-react-app.dev/docs/making-a-progressive-web-app/">https://create-react-app.dev/docs/making-a-progressive-web-app/</a>)</li><li><strong>Stack Overflow</strong> (<a href="https://stackoverflow.com/a/71158835/12211200">https://stackoverflow.com/a/71158835/12211200</a>)</li><li><strong>MDN Notifications API</strong> (<a href="https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API">https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API</a>)</li><li><strong>MDN Push API</strong> (<a href="https://developer.mozilla.org/en-US/docs/Web/API/Push_API">https://developer.mozilla.org/en-US/docs/Web/API/Push_API</a>)</li></ul><p>The code snippets taken above are from the <a href="https://github.com/we-festify/festify">Festify</a>. The best open-source college/university fests and events management framework.</p><h3>Stackademic</h3><p><em>Thank you for reading until the end. Before you go:</em></p><ul><li><em>Please consider </em><strong><em>clapping</em></strong><em> and </em><strong><em>following</em></strong><em> the writer! 👏</em></li><li><em>Follow us on </em><a href="https://twitter.com/stackademichq"><strong><em>Twitter(X)</em></strong></a><em>, </em><a href="https://www.linkedin.com/company/stackademic"><strong><em>LinkedIn</em></strong></a><em>, and </em><a href="https://www.youtube.com/c/stackademic"><strong><em>YouTube</em></strong></a><strong><em>.</em></strong></li><li><em>Visit </em><a href="http://stackademic.com/"><strong><em>Stackademic.com</em></strong></a><em> to find out more about how we are democratizing free programming education around the world.</em></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15de9567aa9a" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/mastering-web-push-notifications-in-react-and-node-one-stop-guide-15de9567aa9a">Mastering Web-Push Notifications in React and Node: One-Stop Guide</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stock Market Orders Matching Engine in NodeJS]]></title>
            <link>https://jindalujjwal0720.medium.com/stock-market-order-book-orders-matching-engine-in-nodejs-3dff82f70080?source=rss-d71bb8057a4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dff82f70080</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[stock-market]]></category>
            <dc:creator><![CDATA[Ujjwal Jindal]]></dc:creator>
            <pubDate>Sun, 03 Dec 2023 16:31:00 GMT</pubDate>
            <atom:updated>2023-12-04T05:09:56.687Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8n1BRy73qHlNAENn" /><figcaption>Photo by Ishant Mishra on Unsplash</figcaption></figure><p>In finance, a secret ingredient keeps stock markets running smoothly. It’s called the <strong>order book</strong>, like the traffic controller of the stock market. Understanding how this order book works can demystify the stock market even if you’re not a financial expert.</p><h4><strong>Why Order Books Matter</strong></h4><p>Think of a stock market as a bustling marketplace where people buy and sell all sorts of things. In this marketplace, the order book is like a giant board that keeps track of what people want to buy and sell. It’s like an enormous to-do list.</p><p>Imagine you want to buy a new video game console. You’d check the price and see if it’s fair, right? That’s precisely what the order book helps traders do. It lists what people are willing to pay to buy something (these are the <strong><em>buy orders</em></strong>) and what they want to get for selling something (these are the <strong>sell orders</strong>).</p><p>With the order book, the stock market would be a safe place. It helps everyone see what’s happening and ensures that buying and selling happen organizationally.</p><p>This blog will explore how the order book works, how it’s built, and why it’s so crucial in simple terms. Whether you’re a stock market pro or just curious about how this all works, let’s dive into the world of stock market order books together.</p><h3>Order Types</h3><p>Let’s kickstart our journey into the stock market by spotlighting two fundamental order types: market orders, limit orders and stop orders. These are like the building blocks of trading strategies, and once you grasp these, you’ll find it easier to explore and understand the intricacies of more advanced order types that can be derived from these basics.</p><h4>Market Orders</h4><p>In the world of trading, there are different ways to buy or sell stocks, and one of the simplest methods is called a market order. Imagine you’re at a pizza place, and you’re starving. You go to the counter and say, “<em>I want a slice of pepperoni pizza, and I want it now!</em>” In trading, that’s kind of what a market order is like.</p><p>Market orders are super fast because they get executed (completed) almost immediately. However, there’s a catch: Since market orders are all about speed, you might not get the exact price you see on the screen when you place the order. Prices in the stock market can change quickly, just like pizza prices might change if it’s a busy night at the pizza place.</p><p>That’s the scoop on market orders. They’re a straightforward way to make a trade, but they come with some uncertainty about the final price.</p><h4>Limit Orders</h4><p>A limit order is a more thoughtful way to buy or sell stocks. Imagine you’re looking for a vintage comic book at a flea market and don’t want to pay too much for it. You set a limit on how much you’re willing to spend and tell the seller, “<em>I’ll buy this comic book, but only if it’s priced at $50 or less.</em>” That’s pretty much how a limit order works in trading.</p><p>Limit orders give you more control over the price you buy or sell a stock for. However, there’s a trade-off: Unlike market orders that execute quickly, limit orders only get completed when the stock’s price matches your limit. If the stock never reaches your specified price, your order may not get filled, and you won’t buy or sell the stock.</p><p>They’re like bargain-hunting in the stock market, where you name your price and patiently wait for the right opportunity.</p><h4>Stop Orders</h4><p>A clever stop-loss order tool helps protect your investments or take advantage of price movements. Think of it like setting a safety net for your stocks or seizing an opportunity when it arises.</p><p>Imagine you own a stock, and it’s doing well, but you’re worried its price might drop, causing you to lose money. That’s where a stop-loss order comes in. You set a specific price point (say $50) and tell the system, “<em>If this stock’s price hits $50, sell it.</em>” It’s like having an emergency button to prevent significant losses.</p><p>On the flip side, you might be watching a stock, waiting for it to increase in value. Instead of sitting in front of your computer all day, you can use a stop-buy order. You set a trigger price (say $60) and tell the system, “<em>If this stock’s price hits $60, buy it for me.</em>” It’s like having a virtual assistant ready to grab a good deal when it pops up.</p><p>Stop orders can be super handy because they let you plan your moves while protecting your investments or capitalizing on opportunities. However, remember that they don’t guarantee a specific price because once the trigger is hit, it behaves like a regular market or limit order.</p><h4>Cancellation of Orders</h4><p><strong>Market Orders:</strong> Market orders are typically irrevocable. Once you place a market order, it gets executed immediately at the current market price. Since there’s no specific price set in advance, there’s no price level at which you can cancel it. Market orders are a commitment to buy or sell whatever the market offers.</p><p><strong>Limit Orders:</strong> Limit orders, on the other hand, can be cancelled under specific conditions. You can cancel a limit order if the stock’s market price has not reached your specified limit price before it gets executed. Once the stock’s price matches your limit, the order becomes active, and you cannot cancel it.</p><p><strong>Stop Orders:</strong> Stop orders are conditional orders that can be cancelled under certain circumstances. You can cancel a stop order if the stock’s price has not reached the trigger price. Once the stock’s price hits the trigger, the stop order becomes active and is treated as a market or limit order, and you cannot cancel it. However, you can cancel the stop order anytime if the trigger price is not reached.</p><h3>Order Execution</h3><p>The orders book generally supports various queues for processing buy and sell orders:</p><ul><li>Cancel Order</li><li>Market Order Buy Queue</li><li>Market Order Sell Queue</li><li>Limit Order Buy Queue</li><li>Limit Order Sell Queue</li><li>Stop Buy Market Order Queue</li><li>Stop Sell Market Order Queue</li><li>Stop Buy Limit Order Queue</li><li>Stop Sell Limit Order Queue</li></ul><p>Orders are processed in the order listed above. Cancel orders are processed first and instantly, followed by market, limit, and stop orders.</p><p>Some of the other rules that apply to the system of order-matching engines are:</p><ul><li>The orders may be partially filled or not filled in the case of limit orders.</li><li>Market orders may be partially filled at different prices.</li><li>An attempt is always made to match a buy order first against existing sell orders.</li><li>Stop Market orders, after hitting the trigger, become a market order. In contrast, Stop Limit orders, after hitting the trigger price, become a limit order with the trigger price as the limit.</li><li>Orders with the highest bid (buy) price are kept at the top of the queue and will be executed first. Orders with the lowest sell (ask) prices will be sold first. For orders with the same ask price, the order that arrives first will be sold first.</li></ul><p>You have now understood the basics of how the orders in the stock market work and are executed. We will now dive deep into the design part of the system using NodeJS and Mongoose ODM for MongoDB.</p><h3>Design and Implementation</h3><p>Let’s define the models as required for the system now or at later stages. All the unnecessary fields and attributes from the models have been removed to increase readability.</p><pre>const OrderSchema = new mongoose.Schema(<br>  {<br>    userId: {<br>      type: mongoose.Schema.Types.ObjectId,<br>      ref: &quot;User&quot;,<br>      required: true,<br>    },<br>    type: {<br>      type: String,<br>      enum: [&quot;market&quot;, &quot;limit&quot;, &quot;stop-loss-market&quot;, &quot;stop-loss-limit&quot;],<br>      required: true,<br>    },<br>    side: {<br>      type: String,<br>      enum: [&quot;buy&quot;, &quot;sell&quot;],<br>      required: true,<br>    },<br>    symbol: {<br>      type: String,<br>      required: true,<br>    },<br>    qty: {<br>      type: Number,<br>      required: true,<br>    },<br>    filledQty: {<br>      type: Number,<br>      required: true,<br>      default: 0,<br>    },<br>    filledAveragePrice: {<br>      type: Number,<br>      required: false,<br>    },<br>    limitPrice: {<br>      type: Number,<br>      required: false,<br>    },<br>    stopPrice: {<br>      type: Number,<br>      required: false,<br>    },<br>    status: {<br>      type: String,<br>      enum: [&quot;open&quot;, &quot;closed&quot;, &quot;cancelled&quot;, &quot;expired&quot;],<br>      required: true,<br>      default: &quot;open&quot;,<br>    },<br>  },<br>  {<br>    timestamps: true,<br>  }<br>);</pre><p>The queue-based order book can be implemented in various ways, but I’ve used basic code design patterns to make it easy to grasp. FIFOQueue and AVLTreeQueue are custom-implemented queues, discussions of which are out of the scope of this blog. You can use whatever type of queuing data structure of your choice.</p><pre>class MarketBuyOrderQueue extends FIFOQueue {}<br>class MarketSellOrderQueue extends FIFOQueue {}<br><br>const orderByNonDecreasing = (a, b) =&gt; {<br>  if (a.unitPrice === b.unitPrice) {<br>    if (a.timestamp === b.timestamp) {<br>      return a._id &lt; b._id;<br>    }<br>    return a.timestamp &lt; b.timestamp;<br>  }<br>  return a.unitPrice &lt; b.unitPrice;<br>};<br><br>const orderByNonIncreasing = (a, b) =&gt; {<br>  if (a.unitPrice === b.unitPrice) {<br>    if (a.timestamp === b.timestamp) {<br>      return a._id &lt; b._id;<br>    }<br>    return a.timestamp &lt; b.timestamp;<br>  }<br>  return a.unitPrice &gt; b.unitPrice;<br>};<br><br>class LimitBuyOrderQueue extends AVLTreeQueue {<br>  constructor() {<br>    super(orderByNonIncreasing); // decreasing order<br>  }<br>}<br>class LimitSellOrderQueue extends AVLTreeQueue {<br>  constructor() {<br>    super(orderByNonDecreasing); // increasing order<br>  }<br>}<br>class StopLossBuyMarketOrderQueue extends AVLTreeQueue {<br>  constructor() {<br>    super(orderByNonIncreasing); // decreasing order<br>  }<br>}<br>class StopLossSellMarketOrderQueue extends AVLTreeQueue {<br>  constructor() {<br>    super(orderByNonDecreasing); // increasing order<br>  }<br>}<br>class StopLossBuyLimitOrderQueue extends AVLTreeQueue {<br>  constructor() {<br>    super(orderByNonIncreasing); // decreasing order<br>  }<br>}<br>class StopLossSellLimitOrderQueue extends AVLTreeQueue {<br>  constructor() {<br>    super(orderByNonDecreasing); // increasing order<br>  }<br>}<br><br>class OrderBook {<br>  constructor() {<br>    this.marketBuyOrderQueue = new MarketBuyOrderQueue();<br>    this.marketSellOrderQueue = new MarketSellOrderQueue();<br>    this.limitBuyOrderQueue = new LimitBuyOrderQueue();<br>    this.limitSellOrderQueue = new LimitSellOrderQueue();<br>    this.stopLossBuyMarketOrderQueue = new StopLossBuyMarketOrderQueue();<br>    this.stopLossSellMarketOrderQueue = new StopLossSellMarketOrderQueue();<br>    this.stopLossBuyLimitOrderQueue = new StopLossBuyLimitOrderQueue();<br>    this.stopLossSellLimitOrderQueue = new StopLossSellLimitOrderQueue();<br>  }<br>}</pre><p>I have used different queues for all kinds of orders, but you can go with any number of queues based on the requirements of your specific engine. Let’s move on to the next part, adding the orders to the queues and cancelling orders from the queues. We will follow the precedence of cancellation discussed above. The code for addition and deletion is self-explanatory and may not need a discussion. You can modify this part accordingly.</p><pre>cancelOrder(order) {<br>    // remove the order from the matching queue<br>    switch (order.type) {<br>      case &quot;market&quot;:<br>        throw new Error(&quot;INVALID_ORDER_TYPE_FOR_CANCEL&quot;);<br>      case &quot;limit&quot;:<br>        if (order.side === &quot;buy&quot;) {<br>          // remove only if filledQty is 0<br>          this.limitBuyOrderQueue.remove(order, (o) =&gt; o.filledQty === 0);<br>        } else {<br>          // remove only if filledQty is 0<br>          this.limitSellOrderQueue.remove(order, (o) =&gt; o.filledQty === 0);<br>        }<br>        return;<br>      case &quot;stop-loss-market&quot;:<br>        if (order.side === &quot;buy&quot;) {<br>          this.stopLossBuyMarketOrderQueue.remove(order);<br>        } else {<br>          this.stopLossSellMarketOrderQueue.remove(order);<br>        }<br>        return;<br>      case &quot;stop-loss-limit&quot;:<br>        if (order.side === &quot;buy&quot;) {<br>          this.stopLossBuyLimitOrderQueue.remove(order);<br>        } else {<br>          this.stopLossSellLimitOrderQueue.remove(order);<br>        }<br>        return;<br>      default:<br>        throw new Error(&quot;INVALID_ORDER_TYPE&quot;);<br>    }<br>  }</pre><pre>addOrder(order) {<br>    // add the order to the matching queue<br>    switch (order.type) {<br>      case &quot;market&quot;:<br>        if (order.side === &quot;buy&quot;) {<br>          this.marketBuyOrderQueue.push(order);<br>        } else {<br>          this.marketSellOrderQueue.push(order);<br>        }<br>        return;<br>      case &quot;limit&quot;:<br>        if (order.side === &quot;buy&quot;) {<br>          this.limitBuyOrderQueue.push(order);<br>        } else {<br>          this.limitSellOrderQueue.push(order);<br>        }<br>        return;<br>      case &quot;stop-loss-market&quot;:<br>        if (order.side === &quot;buy&quot;) {<br>          this.stopLossBuyMarketOrderQueue.push(order);<br>        } else {<br>          this.stopLossSellMarketOrderQueue.push(order);<br>        }<br>        return;<br>      case &quot;stop-loss-limit&quot;:<br>        if (order.side === &quot;buy&quot;) {<br>          this.stopLossBuyLimitOrderQueue.push(order);<br>        } else {<br>          this.stopLossSellLimitOrderQueue.push(order);<br>        }<br>        return;<br>      default:<br>        throw new Error(&quot;INVALID_ORDER_TYPE&quot;);<br>    }<br>  }</pre><h3>Understanding Exchange Algorithms</h3><p>In the fast-paced world of modern stock markets, the efficient matching of buy and sell orders is crucial for maintaining fairness and liquidity. To achieve this, stock exchanges employ sophisticated algorithms that underpin the entire trading process. These exchange algorithms are like the conductors of a symphony, ensuring that transactions happen smoothly and fairly.</p><h4>Order Matching Basics</h4><p>Before diving into exchange algorithms, it’s essential to understand the basics of order matching. Until now, you might have a good understanding of when a trader submits an order to buy or sell a stock, and it enters the exchange’s order book. The exchange’s job is to match these orders to maximize efficiency and transparency.</p><h4>Exchange Algorithm Components</h4><p>Exchange algorithms consist of several key components:</p><ol><li><strong>Price-Time Priority:</strong> Price-time priority is the primary rule for order matching in many stock exchanges. This means that orders with the best (most favourable) prices take precedence, followed by the time of submission. As discussed above, the exchange algorithms ensure that the highest buy order price matches the lowest sell order price.</li><li><strong>Continuous Auction:</strong> Most modern stock exchanges use continuous trading, where orders are matched continuously throughout the day. This allows for real-time price discovery and liquidity.</li><li><strong>Batch Auctions:</strong> In some cases, exchanges use batch auctions, where orders are collected over a short period (e.g., milliseconds) and matched simultaneously. This approach can prevent high-frequency traders from gaining unfair advantages.</li><li><strong>Matching Engine:</strong> The matching engine is at the heart of every exchange, a powerful piece of software responsible for matching buy and sell orders. It uses the exchange’s rules, such as price-time priority, to ensure fair and efficient trading.</li><li><strong>Pre-Trade Risk Controls:</strong> To prevent erroneous or huge orders from disrupting the market, exchanges implement pre-trade risk controls. These controls can include order size limits and price collars.</li></ol><h4>High-Frequency Trading (HFT) and Market Makers</h4><p>In recent years, the rise of high-frequency trading (HFT) and market makers has added complexity to exchange algorithms. HFT firms use lightning-fast computers and algorithms to execute trades in fractions of a second, often capturing tiny price differences. On the other hand, market makers provide liquidity by continuously quoting buy and sell prices.</p><p>Exchange algorithms must strike a delicate balance between accommodating HFT strategies and maintaining fairness for other market participants. Some exchanges even offer colocation services, allowing HFT firms to place their servers physically close to the exchange’s matching engine for faster execution.</p><p>Since I can’t go with all the points mentioned above in this implementation, we will try to implement an exchange algorithm sufficient to handle significant traffic and match the orders efficiently and fairly. You can explore different books and research papers for the algorithms and implement them according to your needs. I first checked all the market-buy and market-sell orders because, as discussed earlier, they are the highest priority.</p><pre>// Matching of market buy and sell<br>matchMarketBuyWithMarketSell = () =&gt; {<br>    let marketBuyOrder = this.marketBuyOrderQueue.front();<br>    let marketSellOrder = this.marketSellOrderQueue.front();<br>    let currentPrice; // TODO: get the current price from the market<br>    while (marketBuyOrder &amp;&amp; marketSellOrder) {<br>      const marketBuyOrderRemainingQty =<br>        marketBuyOrder.qty - marketBuyOrder.filledQty;<br>      const marketSellOrderRemainingQty =<br>        marketSellOrder.qty - marketSellOrder.filledQty;<br>      if (marketBuyOrderRemainingQty === marketSellOrderRemainingQty) {<br>        // both orders will be completely filled<br>        marketBuyOrder.filledQty = marketBuyOrder.qty;<br>        marketSellOrder.filledQty = marketSellOrder.qty;<br>        marketBuyOrder.status = &quot;closed&quot;;<br>        marketSellOrder.status = &quot;closed&quot;;<br>        // calculate the filled average price from previously filled orders<br>        marketBuyOrder.filledAveragePrice =<br>          (marketBuyOrder.filledQty * marketBuyOrder.filledAveragePrice +<br>            marketBuyOrderRemainingQty * currentPrice) /<br>          marketBuyOrder.qty;<br>        marketSellOrder.filledAveragePrice =<br>          (marketSellOrder.filledQty * marketSellOrder.filledAveragePrice +<br>            marketSellOrderRemainingQty * currentPrice) /<br>          marketSellOrder.qty;<br>        // remove the orders from the matching queue<br>        this.marketBuyOrderQueue.pop();<br>        this.marketSellOrderQueue.pop();<br>        // TODO: save the orders to the database<br><br>        // move to the next orders<br>        marketBuyOrder = this.marketBuyOrderQueue.front();<br>        marketSellOrder = this.marketSellOrderQueue.front();<br>      } else if (marketBuyOrderRemainingQty &lt; marketSellOrderRemainingQty) {<br>        // market buy order will be completely filled<br>        // market sell order will be partially filled<br>        marketBuyOrder.filledQty = marketBuyOrder.qty;<br>        marketSellOrder.filledQty += marketBuyOrderRemainingQty; //! check this - might not assign properly in the queue<br>        marketBuyOrder.status = &quot;closed&quot;;<br>        marketSellOrder.status = &quot;open&quot;;<br>        // calculate the filled average price from previously filled orders<br>        marketBuyOrder.filledAveragePrice =<br>          (marketBuyOrder.filledQty * marketBuyOrder.filledAveragePrice +<br>            marketBuyOrderRemainingQty * currentPrice) /<br>          marketBuyOrder.qty;<br>        marketSellOrder.filledAveragePrice =<br>          (marketSellOrder.filledQty * marketSellOrder.filledAveragePrice +<br>            marketBuyOrderRemainingQty * currentPrice) /<br>          marketSellOrder.filledQty;<br>        // remove the market buy order from the matching queue<br>        this.marketBuyOrderQueue.pop();<br>        // TODO: save the market buy order to the database<br><br>        // move to the next market buy order<br>        marketBuyOrder = this.marketBuyOrderQueue.front();<br>      } else {<br>        // market sell order will be completely filled<br>        // market buy order will be partially filled<br>        marketBuyOrder.filledQty += marketSellOrderRemainingQty; //! check this - might not assign properly in the queue<br>        marketSellOrder.filledQty = marketSellOrder.qty;<br>        marketBuyOrder.status = &quot;open&quot;;<br>        marketSellOrder.status = &quot;closed&quot;;<br>        // calculate the filled average price from previously filled orders<br>        marketBuyOrder.filledAveragePrice =<br>          (marketBuyOrder.filledQty * marketBuyOrder.filledAveragePrice +<br>            marketSellOrderRemainingQty * currentPrice) /<br>          marketBuyOrder.filledQty;<br>        marketSellOrder.filledAveragePrice =<br>          (marketSellOrder.filledQty * marketSellOrder.filledAveragePrice +<br>            marketSellOrderRemainingQty * currentPrice) /<br>          marketSellOrder.qty;<br>        // remove the market sell order from the matching queue<br>        this.marketSellOrderQueue.pop();<br>        // TODO: save the market sell order to the database<br><br>        // move to the next market sell order<br>        marketSellOrder = this.marketSellOrderQueue.front();<br>      }<br>    }<br>  };</pre><p>And here goes the first of all, the matching function for the market buy queue with the market sell queue. The code explains using the comments, and there will be some open todos that other services should handle. Similarly, implement all the combinations of queues and take the edge cases. Finally, after combining all the functions based on user counts, average order counts per second, and more factors, the matching service can be called after every new order or every fixed interval based on the requirements.</p><h4>Conclusion</h4><p>As we bring our exploration of stock market order books, market orders, limit orders, and the intricacies of trading to a close, remember that the stock market is a dynamic realm where every order tells a story. From the immediacy of market orders to the precision of limit orders and the strategic foresight of stop orders, each plays a unique role in the drama of buying and selling.</p><p>So, as you embark on your trading journey, may your orders be executed swiftly, your limits be reached, and your stops protect your investments. Here’s to informed decisions, strategic manoeuvres, and a profitable finale in the fascinating world of stocks. Happy trade with your engine! 📈</p><h4>Advanced Reading</h4><p><a href="http://Foundations of Reinforcement Learning with Applications in Finance Ashwin Rao, Tikhon Jelvis">Foundations of Reinforcement Learning with Applications in Finance</a> - <em>by Ashwin Rao, Tikhon Jelvis</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dff82f70080" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>