<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abanoub Kerols</title>
    <description>The latest articles on Forem by Abanoub Kerols (@abanoubkerols).</description>
    <link>https://forem.com/abanoubkerols</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F884154%2F199c573c-3dbe-4ff8-a82f-55cf60ec19e0.jpeg</url>
      <title>Forem: Abanoub Kerols</title>
      <link>https://forem.com/abanoubkerols</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abanoubkerols"/>
    <language>en</language>
    <item>
      <title>Ultimate Practical Guide to RxJS in Angular (2026): From Zero to Pro with Full Examples</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Mon, 13 Apr 2026 15:34:25 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/ultimate-practical-guide-to-rxjs-in-angular-2026-from-zero-to-pro-with-full-examples-5h85</link>
      <guid>https://forem.com/abanoubkerols/ultimate-practical-guide-to-rxjs-in-angular-2026-from-zero-to-pro-with-full-examples-5h85</guid>
      <description>&lt;p&gt;RxJS is not just a library — it’s the backbone of every reactive Angular application. Even with Signals in Angular 16+, RxJS remains the king for HTTP, real-time data, complex forms, typeahead search, and WebSockets.&lt;br&gt;
In this article you will learn:&lt;/p&gt;

&lt;p&gt;Core concepts explained practically&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The 4 flattening operators (switchMap, mergeMap, concatMap, exhaustMap) with clear comparison&lt;/li&gt;
&lt;li&gt;Full ready-to-copy examples (service + component + HTML)&lt;/li&gt;
&lt;li&gt;Advanced RxJS + WebSockets (real-time chat &amp;amp; live updates)&lt;/li&gt;
&lt;li&gt;Common mistakes that kill performance and cause memory leaks&lt;/li&gt;
&lt;li&gt;2026 best practices (toSignal, takeUntilDestroyed, rxResource, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1. Quick Intro: Why RxJS in Angular?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Angular is built on Observables, not Promises:&lt;/li&gt;
&lt;li&gt;HttpClient returns Observable&lt;/li&gt;
&lt;li&gt;Form.valueChanges returns Observable&lt;/li&gt;
&lt;li&gt;Router events, WebSockets, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benefits&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatic cancellation&lt;/li&gt;
&lt;li&gt;Powerful operators&lt;/li&gt;
&lt;li&gt;Centralized error handling&lt;/li&gt;
&lt;li&gt;Seamless integration with Signals (toSignal / toObservable
)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Core Concepts (Fast &amp;amp; Clear)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Observable – Stream of data (cold/hot)&lt;/li&gt;
&lt;li&gt;Observer – Listens with next, error, complete&lt;/li&gt;
&lt;li&gt;Operators – pipe() magic (map, filter, tap, etc.)&lt;/li&gt;
&lt;li&gt;Subject – Observable + Observer (BehaviorSubject is most used)&lt;/li&gt;
&lt;li&gt;Subscription – Must be cleaned (but in 2026 we use takeUntilDestroyed)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. The 4 Flattening Operators – MUST KNOW (2026 Edition)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These operators decide what happens when a new inner Observable arrives while the previous one is still running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;switchMap&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;: Cancels the previous request and starts a new one&lt;br&gt;
&lt;strong&gt;Use Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typeahead search&lt;/li&gt;
&lt;li&gt;Autocomplete&lt;/li&gt;
&lt;li&gt;Scenarios where only the latest value matters&lt;/li&gt;
&lt;li&gt;Cancels previous? Yes&lt;/li&gt;
&lt;li&gt;Order preserved? No &lt;/li&gt;
&lt;li&gt;Performance: Best for this type of scenario &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;switchMap Example (Most Used – Typeahead)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;search$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;debounceTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;term&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;searchUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;term&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// ← cancels old HTTP&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;mergeMap&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;: Runs all requests in parallel&lt;br&gt;
&lt;strong&gt;Use Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Independent API calls&lt;/li&gt;
&lt;li&gt;When order doesn’t matter&lt;/li&gt;
&lt;li&gt;Cancels previous? No &lt;/li&gt;
&lt;li&gt;Order preserved? No &lt;/li&gt;
&lt;li&gt;Performance: Good &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;mergeMap Example (Parallel Requests) TypeScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;getUserAndPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/users/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;mergeMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/posts?userId=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;concatMap&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;: Waits for the previous request to complete before starting the next one&lt;br&gt;
&lt;strong&gt;Use Case:&lt;/strong&gt;&lt;br&gt;
Sequential flows (e.g., login → fetch profile)&lt;br&gt;
Steps that must happen in order&lt;br&gt;
Cancels previous? No&lt;br&gt;
Order preserved? Yes &lt;br&gt;
Performance: Safe but slower &lt;br&gt;
&lt;strong&gt;concatMap Example (Sequential)TypeScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;saveAndRefresh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveForm&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;concatMap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refreshData&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// waits for save to finish&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;exhaustMap&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;: Ignores new requests while the current one is still running&lt;br&gt;
&lt;strong&gt;Use Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Form submissions&lt;/li&gt;
&lt;li&gt;Preventing double clicks on buttons&lt;/li&gt;
&lt;li&gt;Cancels previous? No (it ignores new ones) &lt;/li&gt;
&lt;li&gt;Order preserved? Yes &lt;/li&gt;
&lt;li&gt;Performance: Excellent for forms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;exhaustMap Example (Prevent Double Submit) TypeScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;submitButtonClicked$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Subject&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;submit$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;submitButtonClicked$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;exhaustMap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveData&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// ignores clicks while saving&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Rule of thumb in 2026:&lt;/strong&gt;&lt;br&gt;
90% of the time → switchMap&lt;br&gt;
Parallel → mergeMap&lt;br&gt;
Order matters → concatMap&lt;br&gt;
User actions → exhaustMap&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Example 1: HTTP + switchMap + toSignal (Modern Angular 2026)&lt;br&gt;
user.service.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;providedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;searchUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;term&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;term&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://jsonplaceholder.typicode.com/users?q=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;term&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;search.component.ts (Standalone) TypeScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-search&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;standalone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;CommonModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;FormsModule&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;input [(ngModel)]="query" placeholder="Search users..."&amp;gt;
    &amp;lt;div *ngIf="loading()"&amp;gt;Loading...&amp;lt;/div&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li *ngFor="let user of users()"&amp;gt;{{ user.name }}&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  `&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SearchComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;toSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;toObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;debounceTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
      &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;term&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;searchUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;term&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
      &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
      &lt;span class="nf"&gt;takeUntilDestroyed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;userService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Example 2: Reactive Forms + combineLatest + toSignal TypeScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;toSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;firstName&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueChanges&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lastName&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueChanges&lt;/span&gt;
  &lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Advanced Example: RxJS + WebSockets (Real-time Chat 2026)&lt;br&gt;
websocket.service.ts (Modern &amp;amp; Clean)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WebSocketSubject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rxjs/webSocket&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;takeUntilDestroyed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rxjs/operators&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;providedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WebSocketService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;socket$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WebSocketSubject&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Message&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;wss://your-chat-api.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;messages$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;socket$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;// cache latest message&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;socket$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;socket$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;chat.component.ts (Real-time with Signals) TypeScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;standalone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;div *ngFor="let msg of messages()"&amp;gt;{{ msg.text }}&amp;lt;/div&amp;gt;
    &amp;lt;input #input&amp;gt;
    &amp;lt;button (click)="send(input.value); input.value=''"&amp;gt;Send&amp;lt;/button&amp;gt;
  `&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ChatComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;toSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messages$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WebSocketService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Auto cleanup&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messages$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;takeUntilDestroyed&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pro tip: Use shareReplay(1) + toSignal for instant UI updates without flicker.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Common RxJS Mistakes (and How to Fix Them in 2026)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Forgetting to unsubscribe → Memory leaks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fix: Always use takeUntilDestroyed() (Angular 16+)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using mergeMap instead of switchMap for search → 50 requests fired&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fix: switchMap = cancel previous&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nested subscribe() (subscribe hell)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fix: Use switchMap, concatMap, combineLatest&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not handling errors → App crashes
Fix:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toastr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using async pipe + manual subscribe in same component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fix: Choose one (prefer toSignal in 2026)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cold Observable called multiple times&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fix: Use shareReplay(1) or share() in services&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ignoring exhaustMap on buttons → Double payments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fix: exhaustMap on submit actions&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. 2026 Best Practices (Apply These Today)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use takeUntilDestroyed() everywhere&lt;/li&gt;
&lt;li&gt;Convert to toSignal() for templates (faster than async pipe)&lt;/li&gt;
&lt;li&gt;Use toObservable() when you need RxJS pipeline from a signal&lt;/li&gt;
&lt;li&gt;Centralize error handling with catchError + global interceptor&lt;/li&gt;
&lt;li&gt;shareReplay(1) for cached services&lt;/li&gt;
&lt;li&gt;New rxResource (Angular 19+) for data fetching with signals&lt;/li&gt;
&lt;li&gt;Never subscribe in services — return Observable always&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Final Words&lt;/strong&gt;&lt;br&gt;
RxJS + Signals in Angular 2026 is the most powerful combination ever.&lt;br&gt;
Master switchMap (90% of cases), use WebSocketSubject for real-time, and never forget takeUntilDestroyed.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>rxjs</category>
      <category>angular</category>
    </item>
    <item>
      <title>Socket.IO: The Complete Guide to Building Real-Time Web Applications (2026 Edition)</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Wed, 04 Mar 2026 19:31:44 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/socketio-the-complete-guide-to-building-real-time-web-applications-2026-edition-c7h</link>
      <guid>https://forem.com/abanoubkerols/socketio-the-complete-guide-to-building-real-time-web-applications-2026-edition-c7h</guid>
      <description>&lt;p&gt;Socket.IO is one of the most popular and reliable JavaScript libraries for adding real-time, bidirectional communication to web applications. Whether you're building live chat, multiplayer games, collaborative tools, notifications, live dashboards, or stock tickers, Socket.IO makes it simple and production-ready.&lt;br&gt;
As of February 2026, the latest stable version is 4.8.3 (released December 2025). It remains actively maintained, with excellent TypeScript support and battle-tested features used by countless production apps.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Socket.IO?
&lt;/h2&gt;

&lt;p&gt;Socket.IO enables low-latency, event-based communication between clients (browsers, mobile apps, or even servers) and a Node.js backend. It consists of:&lt;/p&gt;

&lt;p&gt;Server-side library (socket.io for Node.js)&lt;br&gt;
Client-side library (works in browsers via CDN or npm, and even in Node.js)&lt;/p&gt;

&lt;p&gt;Unlike plain WebSockets, Socket.IO is not just a thin wrapper. It adds powerful, ready-to-use features that solve real-world problems automatically.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Socket.IO Beats Plain WebSockets
&lt;/h2&gt;

&lt;p&gt;Plain WebSockets are fast but require you to handle many edge cases yourself. Socket.IO provides these out of the box:&lt;/p&gt;

&lt;p&gt;Automatic transport selection: WebSocket (primary), WebTransport (modern, since v4.7+), fallback to HTTP long-polling&lt;br&gt;
Automatic reconnection with exponential back-off and heartbeat checks&lt;br&gt;
Packet buffering: Events sent while offline are replayed after reconnect&lt;br&gt;
Connection state recovery (since v4.6+): Recover missed events after brief disconnects&lt;br&gt;
Clean event system (emit / on) with acknowledgements (callbacks)&lt;br&gt;
Rooms for group messaging and namespaces for logical separation&lt;br&gt;
Broadcasting to everyone or subsets of clients&lt;br&gt;
Built-in scaling via adapters (Redis, PostgreSQL, etc.)&lt;/p&gt;
&lt;h2&gt;
  
  
  Core Features with Clear Examples
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Basic Connection &amp;amp; Events&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Server (Node.js):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;socket.io&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;io&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;httpServer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`User connected: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// broadcast to all&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;disconnect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`User disconnected: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;httpServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server running on http://localhost:3000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Client (browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/socket.io/socket.io.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Received:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Append to chat UI&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Acknowledgements (Request-Response Style)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;question&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Is pizza good?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server replied:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// "Yes, extra cheese!"&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Server:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;question&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Yes, extra cheese!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Rooms: Group &amp;amp; Private Messaging
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Join/leave rooms dynamically.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;join room&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user joined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; joined &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;room&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// only to room members&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;join room&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;room-42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;room&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;room-42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi team!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For 1-on-1 private chat, use user IDs as personal rooms:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// on connect&lt;/span&gt;
&lt;span class="c1"&gt;// Then: io.to(targetUserId).emit(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Namespaces: Separate Logic Channels
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Server&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;adminNs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;adminNs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Admin-only events here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Client&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;adminSocket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Authentication (JWT Example)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Server (middleware):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handshake&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="c1"&gt;// Verify JWT (using jsonwebtoken)&lt;/span&gt;
  &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;secret&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;your-jwt-here&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Typing Indicators &amp;amp; Presence
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Server (simple online tracking):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;online&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;online&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;online&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;typing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;broadcast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;typing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;disconnect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;online&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;online&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Scaling for Production&lt;/strong&gt;&lt;br&gt;
For multiple servers (load-balanced):&lt;/p&gt;

&lt;p&gt;Use an adapter like Redis:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createAdapter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@socket.io/redis-adapter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;redis&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;redis://localhost:6379&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;duplicate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;pub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()]);&lt;/span&gt;
&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;createAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pub&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now rooms and broadcasts work across all instances.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use Socket.IO?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Perfect for:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Live chat &amp;amp; notifications&lt;br&gt;
Multiplayer games&lt;br&gt;
Collaborative editing&lt;br&gt;
Real-time dashboards / stock prices&lt;br&gt;
Presence systems ("who's online")&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Alternatives if needed:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ultra-high performance: plain ws or WebSockets.js&lt;br&gt;
Managed service: Supabase Realtime, Firebase, Ably, Pusher&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Socket.IO Remains a Top Choice in 2026&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Super intuitive API&lt;br&gt;
Handles fallbacks, reconnects, buffering automatically&lt;br&gt;
Scales well with adapters&lt;br&gt;
Great cross-browser/device support&lt;br&gt;
Huge community &amp;amp; ecosystem&lt;/p&gt;

&lt;p&gt;Socket.IO strikes the perfect balance: easy for beginners, powerful for production. Start with a simple chat, then add rooms, auth, and scaling as your app grows.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding TCP/IP — The Backbone of the Internet (with Node.js Examples)</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Sun, 07 Dec 2025 20:32:27 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/understanding-tcpip-the-backbone-of-the-internet-with-nodejs-examples-432c</link>
      <guid>https://forem.com/abanoubkerols/understanding-tcpip-the-backbone-of-the-internet-with-nodejs-examples-432c</guid>
      <description>&lt;p&gt;The internet might feel magical, but beneath every API request, browser load, or WebSocket message, there’s a structured communication model working silently: TCP/IP.&lt;br&gt;
If you understand TCP/IP, you understand how the internet really works.&lt;/p&gt;

&lt;p&gt;In this article, we’ll break it down clearly — layer by layer — and explore how Node.js interacts with TCP at a low level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is TCP/IP?&lt;/strong&gt;&lt;br&gt;
TCP/IP (Transmission Control Protocol / Internet Protocol) is a suite of protocols that define how data moves across networks.&lt;/p&gt;

&lt;p&gt;It’s made of 4 logical layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Network Access Layer&lt;/li&gt;
&lt;li&gt;Internet Layer (IP)&lt;/li&gt;
&lt;li&gt;Transport Layer (TCP/UDP)&lt;/li&gt;
&lt;li&gt;Application Layer (HTTP, DNS, FTP…)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of thinking of them as strict boxes, think of them as stages data passes through from one machine to another.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Internet Layer — IP Addressing &amp;amp; Routing
&lt;/h2&gt;

&lt;p&gt;The Internet Protocol (IP) is responsible for addressing and routing packets from the sender to the receiver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What IP Does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assigns every device a unique address&lt;/li&gt;
&lt;li&gt;Splits data into packets&lt;/li&gt;
&lt;li&gt;Routes packets across the network&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But IP is unreliable. It does not guarantee:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Order&lt;/li&gt;
&lt;li&gt;Delivery&lt;/li&gt;
&lt;li&gt;Retransmission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's like a postal worker who tries their best… but doesn’t promise anything.&lt;/p&gt;

&lt;p&gt;Example IPs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;192.168.1.10 → Local machine&lt;/li&gt;
&lt;li&gt;8.8.8.8 → Google DNS&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Transport Layer — TCP &amp;amp; UDP
&lt;/h2&gt;

&lt;p&gt;This layer defines how communication happens.&lt;/p&gt;

&lt;p&gt;TCP (Transmission Control Protocol)&lt;br&gt;
TCP ensures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reliable delivery&lt;/li&gt;
&lt;li&gt;Correct order&lt;/li&gt;
&lt;li&gt;No missing data&lt;/li&gt;
&lt;li&gt;No duplicates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Features you should know:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flow Control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TCP adjusts sending speed so the receiver doesn't get overwhelmed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congestion Control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TCP slows down when the network is congested.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reliability Mechanisms&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sequence numbers&lt;/li&gt;
&lt;li&gt;ACKs (acknowledgment packets)&lt;/li&gt;
&lt;li&gt;Retransmission&lt;/li&gt;
&lt;li&gt;3-Way Handshake&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TCP guarantees stable, ordered communication — essential for APIs, web browsing, email, databases, etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  UDP (User Datagram Protocol)
&lt;/h2&gt;

&lt;p&gt;Faster, connectionless, and unreliable — used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Streaming&lt;/li&gt;
&lt;li&gt;Online gaming&lt;/li&gt;
&lt;li&gt;VoIP&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  3. Application Layer — Where Developers Live
&lt;/h2&gt;

&lt;p&gt;At this level you find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP&lt;/li&gt;
&lt;li&gt;HTTPS&lt;/li&gt;
&lt;li&gt;WebSockets&lt;/li&gt;
&lt;li&gt;gRPC&lt;/li&gt;
&lt;li&gt;FTP&lt;/li&gt;
&lt;li&gt;SMTP&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every time your browser sends a GET request, it’s happening on top of TCP&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Ports Fit into This&lt;/strong&gt;&lt;br&gt;
If IP is the building address, ports are apartment numbers.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;80 → HTTP&lt;/li&gt;
&lt;li&gt;443 → HTTPS&lt;/li&gt;
&lt;li&gt;22 → SSH&lt;/li&gt;
&lt;li&gt;3000 → Custom Node.js backend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ports allow multiple services to live on the same machine without interfering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js Examples — Working Directly with TCP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Node.js gives developers native access to TCP sockets via the net module.&lt;/p&gt;

&lt;p&gt;Let’s see how it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1 — Create a TCP Server&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const net = require("net");

const server = net.createServer((socket) =&amp;gt; {
  console.log("Client connected:", socket.remoteAddress, socket.remotePort);

  socket.on("data", (data) =&amp;gt; {
    console.log("Received:", data.toString());
    socket.write("ACK: " + data.toString());
  });

  socket.on("end", () =&amp;gt; {
    console.log("Client disconnected");
  });
});

server.listen(5000, "0.0.0.0", () =&amp;gt; {
  console.log("TCP Server running on port 5000");
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this shows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each client has IP + Port&lt;/li&gt;
&lt;li&gt;TCP ensures messages arrive in order&lt;/li&gt;
&lt;li&gt;Server replies using ACK-like behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2 — Create a TCP Client&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const net = require("net");

const client = net.createConnection(5000, "127.0.0.1", () =&amp;gt; {
  console.log("Connected to server");
  client.write("Hello from client!");
});

client.on("data", (data) =&amp;gt; {
  console.log("Server says:", data.toString());
});

client.on("end", () =&amp;gt; {
  console.log("Disconnected from server");
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run both scripts, you’ll see TCP in action:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connection established&lt;/li&gt;
&lt;li&gt;Data sent&lt;/li&gt;
&lt;li&gt;Data acknowledged&lt;/li&gt;
&lt;li&gt;Connection closed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the same mechanism behind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP&lt;/li&gt;
&lt;li&gt;WebSockets&lt;/li&gt;
&lt;li&gt;Database drivers&lt;/li&gt;
&lt;li&gt;Reverse proxies (Nginx, HAProxy)&lt;/li&gt;
&lt;li&gt;Load balancers&lt;/li&gt;
&lt;li&gt;Microservices traffic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The TCP 3-Way Handshake (Simplified)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client → SYN → Server  
Server → SYN-ACK → Client  
Client → ACK → Server  
Connection established

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This handshake ensures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both sides are online&lt;/li&gt;
&lt;li&gt;Both sides agree on communication parameters&lt;/li&gt;
&lt;li&gt;Sequence numbers are synchronized&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why TCP/IP Matters for Developers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding TCP/IP helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Optimize APIs&lt;/li&gt;
&lt;li&gt; Diagnose latency&lt;/li&gt;
&lt;li&gt; Understand why packets drop&lt;/li&gt;
&lt;li&gt; Build scalable distributed systems&lt;/li&gt;
&lt;li&gt; Debug real-world networking issues&lt;/li&gt;
&lt;li&gt; Write high-performance backend services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if you work with frameworks, TCP/IP is happening under the hood every time your code “talks” to anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;TCP/IP isn’t just a networking topic — it’s the foundation of modern computing.&lt;br&gt;
Everything from microservices, databases, browsers, and cloud systems relies on it.&lt;/p&gt;

&lt;p&gt;When you understand TCP/IP, you understand how the internet itself actually works.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Mastering Nest.js: From Core Concepts to a Real-World Project</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Mon, 10 Nov 2025 19:24:24 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/mastering-nestjs-from-core-concepts-to-a-real-world-project-2d2c</link>
      <guid>https://forem.com/abanoubkerols/mastering-nestjs-from-core-concepts-to-a-real-world-project-2d2c</guid>
      <description>&lt;h2&gt;
  
  
  What is Nest.js?
&lt;/h2&gt;

&lt;p&gt;Nest.js is a progressive Node.js framework for building efficient, scalable, and maintainable back-end applications.&lt;br&gt;
It’s built with TypeScript and heavily inspired by Angular’s modular architecture, which makes it a great choice for developers who value structure, testability, and clean code&lt;/p&gt;
&lt;h2&gt;
  
  
  Core Concepts and Features
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Modules&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Nest.js application is made up of modules — the building blocks that organize code into reusable units.&lt;br&gt;
Each module can contain controllers, services, and other providers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
  controllers: [UsersController],
  providers: [UsersService],
})
export class UsersModule {}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Dependency Injection (DI)&lt;/strong&gt;&lt;br&gt;
Dependency Injection is one of Nest’s most powerful features.&lt;br&gt;
It allows classes (like controllers or services) to automatically receive their dependencies without manually creating them&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@nestjs/common';

@Injectable()
export class UsersService {
  findAll() {
    return ['user1', 'user2', 'user3'];
  }
}

import { Controller, Get } from '@nestjs/common';
import { UsersService } from './users.service';

@Controller('users')
export class UsersController {
  constructor(private usersService: UsersService) {}

  @Get()
  getUsers() {
    return this.usersService.findAll();
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, UsersController automatically receives an instance of UsersService through DI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Guards&lt;/strong&gt;&lt;br&gt;
Guards are used to control access to routes based on certain conditions, like authentication or user roles.&lt;br&gt;
They decide whether a request will be processed or rejected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const request = context.switchToHttp().getRequest();
    return request.headers.authorization === 'Bearer mytoken';
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To apply it:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard } from './auth.guard';

@Controller('profile')
export class ProfileController {
  @Get()
  @UseGuards(AuthGuard)
  getProfile() {
    return { name: 'John Doe', role: 'admin' };
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Interceptors&lt;/strong&gt;&lt;br&gt;
Interceptors can transform requests or responses, handle logging, or measure performance before or after a route handler is executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&amp;lt;any&amp;gt; {
    console.log('Before...');
    const now = Date.now();
    return next.handle().pipe(tap(() =&amp;gt; console.log(`After... ${Date.now() - now}ms`)));
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Apply it globally or to a single route:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Get, UseInterceptors } from '@nestjs/common';
import { LoggingInterceptor } from './logging.interceptor';

@Controller('tasks')
@UseInterceptors(LoggingInterceptor)
export class TasksController {
  @Get()
  getTasks() {
    return ['Task 1', 'Task 2'];
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Services&lt;/strong&gt;&lt;br&gt;
Services handle business logic and data manipulation.&lt;br&gt;
They keep controllers clean and focused on handling HTTP requests&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class TasksService {
  private tasks = ['Learn Nest.js', 'Build an API'];

  getAllTasks() {
    return this.tasks;
  }

  addTask(task: string) {
    this.tasks.push(task);
    return this.tasks;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why Developers Choose Nest.js&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TypeScript by default – better tooling and safety&lt;/li&gt;
&lt;li&gt;Modular and testable architecture&lt;/li&gt;
&lt;li&gt;Built-in DI, Guards, Interceptors, and Pipes&lt;/li&gt;
&lt;li&gt;Great integration with TypeORM, GraphQL, and WebSockets&lt;/li&gt;
&lt;li&gt;Perfect for enterprise-grade and scalable apps&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Project Example: Simple To-Do API with Nest.js
&lt;/h2&gt;

&lt;p&gt;Let’s build a simple To-Do API that allows users to view, add, and delete tasks.&lt;br&gt;
We’ll use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Service - to handle business logic&lt;/li&gt;
&lt;li&gt;Controller -  to handle HTTP routes&lt;/li&gt;
&lt;li&gt;Guard - to protect routes with a fake token&lt;/li&gt;
&lt;li&gt;Interceptor - to log requests and responses&lt;/li&gt;
&lt;li&gt;Module - to organize everything&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Project Structure&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
 ├── app.module.ts
 ├── todos/
 │    ├── todos.module.ts
 │    ├── todos.controller.ts
 │    ├── todos.service.ts
 │    ├── auth.guard.ts
 │    └── logging.interceptor.ts
 └── main.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1. todos.service.ts&lt;/strong&gt;&lt;br&gt;
Handles all the business logic and data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@nestjs/common';

@Injectable()
export class TodosService {
  private todos = [{ id: 1, title: 'Learn Nest.js' }];

  findAll() {
    return this.todos;
  }

  add(title: string) {
    const newTodo = { id: Date.now(), title };
    this.todos.push(newTodo);
    return newTodo;
  }

  delete(id: number) {
    this.todos = this.todos.filter(todo =&amp;gt; todo.id !== id);
    return { message: 'Deleted successfully' };
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. todos.controller.ts&lt;/strong&gt;&lt;br&gt;
Defines the REST API endpoints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Get, Post, Body, Delete, Param, UseGuards, UseInterceptors } from '@nestjs/common';
import { TodosService } from './todos.service';
import { AuthGuard } from './auth.guard';
import { LoggingInterceptor } from './logging.interceptor';

@Controller('todos')
@UseGuards(AuthGuard)
@UseInterceptors(LoggingInterceptor)
export class TodosController {
  constructor(private todosService: TodosService) {}

  @Get()
  getTodos() {
    return this.todosService.findAll();
  }

  @Post()
  addTodo(@Body('title') title: string) {
    return this.todosService.add(title);
  }

  @Delete(':id')
  removeTodo(@Param('id') id: number) {
    return this.todosService.delete(id);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. auth.guard.ts&lt;/strong&gt;&lt;br&gt;
Blocks access unless the request has a valid token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const req = context.switchToHttp().getRequest();
    const auth = req.headers.authorization;
    if (auth === 'Bearer mytoken') return true;
    throw new UnauthorizedException('Invalid or missing token');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. logging.interceptor.ts&lt;/strong&gt;&lt;br&gt;
Logs how long each request takes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&amp;lt;any&amp;gt; {
    const now = Date.now();
    console.log('Incoming request...');
    return next.handle().pipe(
      tap(() =&amp;gt; console.log(`Response sent after ${Date.now() - now}ms`)),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. todos.module.ts&lt;/strong&gt;&lt;br&gt;
Brings everything together for this feature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { TodosController } from './todos.controller';
import { TodosService } from './todos.service';
import { AuthGuard } from './auth.guard';
import { LoggingInterceptor } from './logging.interceptor';

@Module({
  controllers: [TodosController],
  providers: [TodosService, AuthGuard, LoggingInterceptor],
})
export class TodosModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. app.module.ts&lt;/strong&gt;&lt;br&gt;
The main application module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { TodosModule } from './todos/todos.module';

@Module({
  imports: [TodosModule],
})
export class AppModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. main.ts&lt;/strong&gt;&lt;br&gt;
Bootstrap the Nest.js application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
  console.log('Server running on http://localhost:3000');
}
bootstrap();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing the API
&lt;/h2&gt;

&lt;p&gt;Use Postman or curl:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get all todos&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET http://localhost:3000/todos
Authorization: Bearer mytoken
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add a todo&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST http://localhost:3000/todos
Authorization: Bearer mytoken
Body: { "title": "Build awesome projects" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete a todo&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE http://localhost:3000/todos/1
Authorization: Bearer mytoken
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This simple To-Do API demonstrates how Nest.js lets you write clean, modular, and testable code using modern features&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Services handle business logic&lt;/li&gt;
&lt;li&gt;Guards secure endpoints&lt;/li&gt;
&lt;li&gt;Interceptors handle cross-cutting concerns&lt;/li&gt;
&lt;li&gt;Dependency Injection makes everything easily testable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nest.js turns back-end development into a structured, elegant, and maintainable experience.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advanced SOLID Principles in a Blogging Platform with JavaScript</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Sun, 12 Oct 2025 14:06:58 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/advanced-solid-principles-in-a-blogging-platform-with-javascript-1elc</link>
      <guid>https://forem.com/abanoubkerols/advanced-solid-principles-in-a-blogging-platform-with-javascript-1elc</guid>
      <description>&lt;p&gt;The SOLID principles—Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion—are essential for crafting maintainable and scalable object-oriented code. This article dives deeper into applying these principles in a blogging platform, using modern JavaScript features like async/await and ES modules. We’ll explore complex, real-world scenarios, such as managing blog posts, user authentication, and comment moderation, with asynchronous operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Single Responsibility Principle (SRP)&lt;/strong&gt;&lt;br&gt;
Definition: A class should have only one reason to change, focusing on a single responsibility.&lt;/p&gt;

&lt;p&gt;Real-World Scenario: Blog Post Management&lt;/p&gt;

&lt;p&gt;In a blogging platform, a BlogPost class might handle multiple tasks: creating posts, publishing them, and sending notifications.&lt;/p&gt;

&lt;p&gt;Bad Example&lt;br&gt;
A class with multiple responsibilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//blog-post.js
export class BlogPost {
  constructor(title, content, author) {
    this.title = title;
    this.content = content;
    this.author = author;
  }

  async saveToDatabase() {
    console.log(`Saving post "${this.title}" to database`);
    // Simulate async DB operation
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 1000));
  }

  async publish() {
    console.log(`Publishing post "${this.title}"`);
    // Simulate async publishing
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 500));
  }

  async notifySubscribers() {
    console.log(`Notifying subscribers about "${this.title}"`);
    // Simulate async notification
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 300));
  }
}

(async () =&amp;gt; {
  const post = new BlogPost("SOLID Principles", "Content...", "Alice");
  await post.saveToDatabase();
  await post.publish();
  await post.notifySubscribers();
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The BlogPost class handles saving, publishing, and notifications, violating SRP.&lt;/p&gt;

&lt;p&gt;Good Example&lt;br&gt;
Separate responsibilities into distinct classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// blog-post.js
export class BlogPost {
  constructor(title, content, author) {
    this.title = title;
    this.content = content;
    this.author = author;
  }

  getDetails() {
    return { title: this.title, content: this.content, author: this.author };
  }
}

// post-repository.js
export class PostRepository {
  async save(post) {
    console.log(`Saving post "${post.getDetails().title}" to database`);
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 1000));
  }
}

// post-publisher.js
export class PostPublisher {
  async publish(post) {
    console.log(`Publishing post "${post.getDetails().title}"`);
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 500));
  }
}

// notification-service.js
export class NotificationService {
  async notify(post) {
    console.log(`Notifying subscribers about "${post.getDetails().title}"`);
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 300));
  }
}

// main.js
import { BlogPost } from './blog-post.js';
import { PostRepository } from './post-repository.js';
import { PostPublisher } from './post-publisher.js';
import { NotificationService } from './notification-service.js';

(async () =&amp;gt; {
  const post = new BlogPost("SOLID Principles", "Content...", "Alice");
  const repository = new PostRepository();
  const publisher = new PostPublisher();
  const notifier = new NotificationService();

  await repository.save(post);
  await publisher.publish(post);
  await notifier.notify(post);
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each class has a single responsibility, improving maintainability and testability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Open/Closed Principle (OCP)&lt;/strong&gt;&lt;br&gt;
Definition: Classes should be open for extension but closed for modification.&lt;/p&gt;

&lt;p&gt;Real-World Scenario: Comment Moderation&lt;/p&gt;

&lt;p&gt;A blogging platform needs to moderate comments based on different policies (e.g., spam filtering, profanity checks).&lt;/p&gt;

&lt;p&gt;Bad Example&lt;br&gt;
Modifying the class for new moderation policies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class CommentModerator {
  moderate(comment, policy) {
    if (policy === "spam") {
      console.log(`Checking "${comment}" for spam`);
      return !comment.includes("http");
    } else if (policy === "profanity") {
      console.log(`Checking "${comment}" for profanity`);
      return !comment.includes("badword");
    }
    return true;
  }
}

const moderator = new CommentModerator();
console.log(moderator.moderate("Check this link: http://example.com", "spam")); // false
console.log(moderator.moderate("This is a badword comment", "profanity")); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding a new policy (e.g., length check) requires modifying CommentModerator.&lt;/p&gt;

&lt;p&gt;Good Example&lt;br&gt;
Use a strategy pattern for extensibility:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class CommentModerator {
  moderate(comment, policy) {
    return policy.apply(comment);
  }
}

export class SpamPolicy {
  apply(comment) {
    console.log(`Checking "${comment}" for spam`);
    return !comment.includes("http");
  }
}

export class ProfanityPolicy {
  apply(comment) {
    console.log(`Checking "${comment}" for profanity`);
    return !comment.includes("badword");
  }
}

const moderator = new CommentModerator();
const spamPolicy = new SpamPolicy();
const profanityPolicy = new ProfanityPolicy();

console.log(moderator.moderate("Check this link: http://example.com", spamPolicy)); // false
console.log(moderator.moderate("This is a badword comment", profanityPolicy)); // false

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New policies can be added by creating new classes, adhering to OCP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Liskov Substitution Principle (LSP)&lt;/strong&gt;&lt;br&gt;
Definition: Subclasses should be substitutable for their base classes without breaking functionality.&lt;/p&gt;

&lt;p&gt;Real-World Scenario: Content Types&lt;/p&gt;

&lt;p&gt;A blogging platform supports different content types (e.g., articles, videos) that need rendering.&lt;/p&gt;

&lt;p&gt;Bad Example&lt;br&gt;
A subclass that breaks substitution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class Content {
  render() {
    console.log("Rendering content in HTML");
  }
}

export class VideoContent extends Content {
  render() {
    throw new Error("Video content requires a player, not HTML!");
  }
}

function displayContent(content) {
  content.render();
}

const article = new Content();
const video = new VideoContent();

displayContent(article); // Works
displayContent(video); // Throws error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VideoContent cannot substitute Content due to the error.&lt;/p&gt;

&lt;p&gt;Good Example&lt;br&gt;
Use a more general abstraction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class Content {
  display() {
    console.log("Displaying content...");
  }
}

export class ArticleContent extends Content {
  display() {
    console.log("Rendering article in HTML");
  }
}

export class VideoContent extends Content {
  display() {
    console.log("Playing video in media player");
  }
}

function displayContent(content) {
  content.display();
}

const article = new ArticleContent();
const video = new VideoContent();

displayContent(article); // Rendering article in HTML
displayContent(video); // Playing video in media player
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both subclasses can substitute Content seamlessly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Interface Segregation Principle (ISP)&lt;/strong&gt;&lt;br&gt;
Definition: Clients should not be forced to implement interfaces they don’t need.&lt;/p&gt;

&lt;p&gt;Real-World Scenario: User Authentication&lt;/p&gt;

&lt;p&gt;Users may authenticate via different methods (e.g., email/password, OAuth), but not all methods require multi-factor authentication (MFA).&lt;/p&gt;

&lt;p&gt;Bad Example&lt;br&gt;
A single interface forces unnecessary implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class AuthMethod {
  login() {
    throw new Error("Method not implemented");
  }
  enableMFA() {
    throw new Error("Method not implemented");
  }
}

export class EmailAuth extends AuthMethod {
  login() {
    console.log("Logging in with email/password");
  }
  enableMFA() {
    console.log("Enabling MFA for email");
  }
}

export class OAuth extends AuthMethod {
  login() {
    console.log("Logging in with OAuth");
  }
  enableMFA() {
    throw new Error("OAuth doesn't support MFA!");
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OAuth is forced to implement enableMFA, which it doesn’t need.&lt;/p&gt;

&lt;p&gt;Good Example&lt;br&gt;
Split interfaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class Loginable {
  login() {
    throw new Error("Method not implemented");
  }
}

export class MFA {
  enableMFA() {
    throw new Error("Method not implemented");
  }
}

export class EmailAuth extends Loginable {
  login() {
    console.log("Logging in with email/password");
  }

  enableMFA() {
    console.log("Enabling MFA for email");
  }
}

export class OAuth extends Loginable {
  login() {
    console.log("Logging in with OAuth");
  }
}

const emailAuth = new EmailAuth();
const oauth = new OAuth();

emailAuth.login(); // Logging in with email/password
emailAuth.enableMFA(); // Enabling MFA for email
oauth.login(); // Logging in with OAuth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OAuth only implements Loginable, adhering to ISP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Dependency Inversion Principle (DIP)&lt;/strong&gt;&lt;br&gt;
Definition: High-level modules should depend on abstractions, not concrete implementations.&lt;/p&gt;

&lt;p&gt;Real-World Scenario: Analytics Tracking&lt;/p&gt;

&lt;p&gt;A blogging platform tracks user interactions (e.g., page views) using different analytics providers.&lt;/p&gt;

&lt;p&gt;Bad Example&lt;br&gt;
Direct dependency on a concrete class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class GoogleAnalytics {
  async track(event) {
    console.log(`Tracking "${event}" with Google Analytics`);
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 200));
  }
}

export class AnalyticsService {
  constructor() {
    this.tracker = new GoogleAnalytics();
  }

  async logEvent(event) {
    await this.tracker.track(event);
  }
}

(async () =&amp;gt; {
  const analytics = new AnalyticsService();
  await analytics.logEvent("Page View");
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AnalyticsService is tightly coupled to GoogleAnalytics.&lt;/p&gt;

&lt;p&gt;Good Example&lt;br&gt;
Depend on an abstraction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class AnalyticsProvider {
  async track(event) {
    throw new Error("Method not implemented");
  }
}

export class GoogleAnalytics extends AnalyticsProvider {
  async track(event) {
    console.log(`Tracking "${event}" with Google Analytics`);
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 200));
  }
}

export class MixpanelAnalytics extends AnalyticsProvider {
  async track(event) {
    console.log(`Tracking "${event}" with Mixpanel`);
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 200));
  }
}

export class AnalyticsService {
  constructor(provider) {
    this.tracker = provider;
  }

  async logEvent(event) {
    await this.tracker.track(event);
  }
}

(async () =&amp;gt; {
  const google = new GoogleAnalytics();
  const mixpanel = new MixpanelAnalytics();

  const analytics1 = new AnalyticsService(google);
  const analytics2 = new AnalyticsService(mixpanel);

  await analytics1.logEvent("Page View"); // Tracking with Google Analytics
  await analytics2.logEvent("Page View"); // Tracking with Mixpanel
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AnalyticsService depends on the AnalyticsProvider abstraction, enabling flexibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Applying SOLID principles in a blogging platform using modern JavaScript features results in modular, maintainable, and scalable code. By adhering to SRP, OCP, LSP, ISP, and DIP, developers can handle complex requirements like asynchronous operations and diverse integrations with ease.Use these principles to build robust applications that adapt to changing requirements.&lt;/p&gt;

</description>
      <category>solidprinciples</category>
    </item>
    <item>
      <title>Advanced Guide to Signals and Closures in Angular</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Fri, 15 Aug 2025 17:34:42 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/advanced-guide-to-signals-and-closures-in-angular-3k3k</link>
      <guid>https://forem.com/abanoubkerols/advanced-guide-to-signals-and-closures-in-angular-3k3k</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Angular Signals, introduced in Angular 16, have transformed state management in Angular applications by offering a reactive, fine-grained approach to change detection. Paired with JavaScript closures—a mechanism allowing functions to retain access to their lexical scope—signals enable developers to build performant, maintainable, and intuitive applications. This article explores Angular Signals in depth, their synergy with closures, advanced use cases, and best practices for leveraging these features effectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deep Dive into Angular Signals&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Signals are a reactive primitive in Angular that hold a value and notify subscribers when that value changes. They are designed to optimize change detection, reduce reliance on Zone.js, and pave the way for zoneless Angular applications. Signals are particularly useful for managing state in a reactive, predictable manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Signal Concepts&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Signal Creation: Use the &lt;code&gt;signal()&lt;/code&gt; function to create a writable signal with an initial value.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { signal } from '@angular/core';
const counter = signal(0); // Writable signal with initial value 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Reading and Updating Signals:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Access a signal's value by calling it as a function: &lt;code&gt;counter()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Update using &lt;code&gt;set()&lt;/code&gt; for direct assignment or&lt;code&gt;update()&lt;/code&gt; for      functional updates
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;counter.set(5); // Set to 5
counter.update(value =&amp;gt; value + 1); // Increment to 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Computed Signals: A computed() signal derives its value from other signals and updates automatically when dependencies change.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const doubled = computed(() =&amp;gt; counter() * 2); // Updates when counter changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Effects: The effect() function runs side effects (e.g., logging, API calls) when dependent signals change.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;effect(() =&amp;gt; console.log(`Counter is now: ${counter()}`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;eadonly Signals: Use asReadonly() to create a read-only version of a signal to prevent accidental mutations
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const readonlyCounter = counter.asReadonly();
console.log(readonlyCounter()); // Can read, but cannot set/update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Signals in Angular Components&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Signals integrate seamlessly with Angular’s template-driven architecture. Here’s an example of a counter component with signals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, signal, computed } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `
    &amp;lt;p&amp;gt;Count: {{ count() }}&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;Is Even: {{ isEven() }}&amp;lt;/p&amp;gt;
    &amp;lt;button (click)="increment()"&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;button (click)="reset()"&amp;gt;Reset&amp;lt;/button&amp;gt;
  `
})
export class CounterComponent {
  count = signal(0);
  isEven = computed(() =&amp;gt; this.count() % 2 === 0);

  increment() {
    this.count.update(value =&amp;gt; value + 1);
  }

  reset() {
    this.count.set(0);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In this example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;count is a writable signal tracking the counter value.&lt;/li&gt;
&lt;li&gt;isEven is a computed signal that reacts to changes in count.&lt;/li&gt;
&lt;li&gt;The template updates automatically when count changes, demonstrating fine-grained reactivity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advanced Signal Features&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Signal Equality Functions&lt;/p&gt;

&lt;p&gt;Signals support custom equality functions to optimize updates. By default, signals use strict equality &lt;code&gt;(===)&lt;/code&gt; to determine if a new value warrants an update. You can override this behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { signal } from '@angular/core';

interface User { id: number; name: string }
const user = signal&amp;lt;User&amp;gt;({ id: 1, name: 'John' }, { equal: (a, b) =&amp;gt; a.id === b.id });
user.set({ id: 1, name: 'Jane' }); // No update triggered (same id)
user.set({ id: 2, name: 'Jane' }); // Update triggered (different id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful for complex objects where only specific properties determine equality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Signal Mutations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For signals holding objects or arrays, the mutate() method allows in-place updates to avoid unnecessary object creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const todos = signal&amp;lt;string[]&amp;gt;([]);
todos.mutate(list =&amp;gt; list.push('New Task')); // Modifies array in place
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Signals with RxJS Interoperability&lt;/strong&gt;&lt;br&gt;
Angular Signals can interoperate with RxJS, enabling integration with observables. The&lt;code&gt;@angular/core/rxjs-interop&lt;/code&gt; package provides utilities like &lt;code&gt;toSignal()&lt;/code&gt; and &lt;code&gt;toObservable()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { interval } from 'rxjs';

@Component({
  selector: 'app-timer',
  template: `&amp;lt;p&amp;gt;Seconds: {{ seconds() }}&amp;lt;/p&amp;gt;`
})
export class TimerComponent {
  seconds = toSignal(interval(1000), { initialValue: 0 });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;toSignal()&lt;/code&gt; converts an RxJS observable into a signal, allowing reactive updates in the template.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Closures in JavaScript and Angular&lt;/strong&gt;&lt;br&gt;
A closure is a function that retains access to its lexical scope, even when executed outside that scope. Closures are a cornerstone of JavaScript and play a critical role in Angular applications, especially when working with signals, event handlers, and reactive patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Closure Basics&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createCounter() {
  let count = 0;
  return function increment() {
    count++;
    return count;
  };
}

const counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;increment&lt;/code&gt;function is a closure that retains access to &lt;code&gt;count&lt;/code&gt;, even after &lt;code&gt;createCounter&lt;/code&gt;finishes executing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Closures in Angular&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Closures are pervasive in Angular, appearing in event handlers, computed signals, and service methods. They allow functions to maintain state without exposing it globally.&lt;/p&gt;

&lt;p&gt;Closures with Signals&lt;/p&gt;

&lt;p&gt;Computed signals and effects often rely on closures. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, signal, computed } from '@angular/core';

@Component({
  selector: 'app-profile',
  template: `
    &amp;lt;input [(ngModel)]="name" (ngModelChange)="nameSignal.set($event)" /&amp;gt;
    &amp;lt;p&amp;gt;Welcome, {{ formattedName() }}!&amp;lt;/p&amp;gt;
  `
})
export class ProfileComponent {
  nameSignal = signal('Guest');
  formattedName = computed(() =&amp;gt; {
    // Closure: retains access to nameSignal
    return this.nameSignal().toUpperCase();
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;formattedName&lt;/code&gt;computed signal is a closure that accesses &lt;code&gt;nameSignal&lt;/code&gt;and recomputes when the signal changes.&lt;/p&gt;

&lt;p&gt;Closures in Event Handlers&lt;/p&gt;

&lt;p&gt;Event handlers in Angular components often form closures over component properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';

@Component({
  selector: 'app-toggle',
  template: `&amp;lt;button (click)="toggle()"&amp;gt;Toggle: {{ isActive }}&amp;lt;/button&amp;gt;`
})
export class ToggleComponent {
  isActive = false;

  toggle() {
    // Closure: retains access to isActive
    this.isActive = !this.isActive;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;toggle&lt;/code&gt;method closes over &lt;code&gt;isActive&lt;/code&gt;, allowing it to modify the component’s state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Closure Patterns&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Factory Functions&lt;/p&gt;

&lt;p&gt;Closures can be used to create factory functions that encapsulate private state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class CounterService {
  createCounter() {
    let count = 0;
    return {
      increment: () =&amp;gt; ++count,
      getCount: () =&amp;gt; count
    };
  }
}

@Component({
  selector: 'app-counter',
  template: `&amp;lt;p&amp;gt;Count: {{ count }}&amp;lt;/p&amp;gt;&amp;lt;button (click)="increment()"&amp;gt;Increment&amp;lt;/button&amp;gt;`
})
export class CounterComponent {
  counter = this.counterService.createCounter();
  count = 0;

  constructor(private counterService: CounterService) {}

  increment() {
    this.count = this.counter.increment();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;createCounter&lt;/code&gt;returns an object with methods that close over the private &lt;code&gt;count&lt;/code&gt;variable, ensuring encapsulation.&lt;/p&gt;

&lt;p&gt;Memoization with Closures&lt;/p&gt;

&lt;p&gt;Closures can be used to memoize expensive computations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function memoize(fn: (n: number) =&amp;gt; number) {
  const cache: { [key: number]: number } = {};
  return (n: number) =&amp;gt; {
    if (n in cache) return cache[n];
    return (cache[n] = fn(n));
  };
}

const fibonacci = memoize(n =&amp;gt; (n &amp;lt;= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2)));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In an Angular context, this can optimize computed signals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const expensiveValue = computed(() =&amp;gt; {
  const memoized = memoize(n =&amp;gt; /* expensive computation */);
  return memoized(this.inputSignal());
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Performance Considerations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Signals&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fine-Grained Reactivity: Signals update only the parts of the UI affected by state changes, reducing unnecessary DOM operations.&lt;/li&gt;
&lt;li&gt;Avoid Overuse of Effects: Effects can lead to performance issues if overused, as they run synchronously on signal changes. Use them sparingly for side effects like logging or API calls.&lt;/li&gt;
&lt;li&gt;Computed Signal Optimization: Computed signals cache their values and only recompute when dependencies change, making them efficient for derived state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Closures&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory Management:&lt;/strong&gt; Closures can cause memory leaks if they retain references to large objects. Always clean up resources (e.g., intervals, subscriptions) in &lt;code&gt;ngOnDestroy&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid Excessive Nesting&lt;/strong&gt;: Deeply nested closures can make code harder to maintain and debug. Keep closure hierarchies shallow where possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example: Cleaning Up Closures&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, OnDestroy } from '@angular/core';
import { interval } from 'rxjs';
import { toSignal } from '@angular/core/rxjs-interop';

@Component({
  selector: 'app-timer',
  template: `&amp;lt;p&amp;gt;Seconds: {{ seconds() }}&amp;lt;/p&amp;gt;`
})
export class TimerComponent implements OnDestroy {
  private interval$ = interval(1000);
  seconds = toSignal(this.interval$, { initialValue: 0 });

  ngOnDestroy() {
    // Cleanup not needed for toSignal, but shown for other closure-based resources
    this.interval$.subscribe().unsubscribe();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Common Pitfalls and Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Signals&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Avoid Direct Mutations&lt;/strong&gt;: Always use &lt;code&gt;set()&lt;/code&gt;, &lt;code&gt;update()&lt;/code&gt;, or &lt;code&gt;mutate()&lt;/code&gt; to change signal values to ensure reactivity.&lt;/li&gt;
&lt;li&gt;Use Readonly Signals: Expose signals as readonly to services or child components to prevent unintended updates.&lt;/li&gt;
&lt;li&gt;Test Signal Dependencies: Ensure computed signals and effects only depend on necessary signals to avoid unnecessary recomputations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Closures&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Watch for Memory Leaks: Be cautious with closures in long-lived components or services, as they can hold onto references indefinitely.&lt;/li&gt;
&lt;li&gt;Debugging Closures: Use tools like Chrome DevTools to inspect closure scopes and identify unintended variable captures.&lt;/li&gt;
&lt;li&gt;Keep Closures Simple: Avoid complex logic within closures to improve readability and maintainability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Real-World Example: Todo List with Signals and Closures&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here’s a complete example of a todo list application using signals and closures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, signal, computed, effect } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-todo-list',
  standalone: true,
  imports: [FormsModule],
  template: `
    &amp;lt;input [ngModel]="newTodo()" (ngModelChange)="newTodo.set($event)" placeholder="Add todo" /&amp;gt;
    &amp;lt;button (click)="addTodo()"&amp;gt;Add&amp;lt;/button&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li *ngFor="let todo of todos(); let i = index"&amp;gt;
        &amp;lt;ng-container *ngIf="editingIndex() === i; else viewMode"&amp;gt;
          &amp;lt;input [ngModel]="editTodo()" (ngModelChange)="editTodo.set($event)" /&amp;gt;
          &amp;lt;button (click)="saveEdit(i)"&amp;gt;Save&amp;lt;/button&amp;gt;
          &amp;lt;button (click)="cancelEdit()"&amp;gt;Cancel&amp;lt;/button&amp;gt;
        &amp;lt;/ng-container&amp;gt;
        &amp;lt;ng-template #viewMode&amp;gt;
          {{ todo }}
          &amp;lt;button (click)="startEdit(i, todo)"&amp;gt;Edit&amp;lt;/button&amp;gt;
          &amp;lt;button (click)="removeTodo(i)"&amp;gt;Remove&amp;lt;/button&amp;gt;
        &amp;lt;/ng-template&amp;gt;
      &amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
    &amp;lt;p&amp;gt;Total: {{ totalCount() }}&amp;lt;/p&amp;gt;
  `
})
export class TodoListComponent {
  newTodo = signal('');
  editTodo = signal('');
  todos = signal&amp;lt;string[]&amp;gt;([]);
  totalCount = computed(() =&amp;gt; this.todos().length);
  editingIndex = signal&amp;lt;number | null&amp;gt;(null);

  constructor() {
    effect(() =&amp;gt; console.log(`Todos updated: ${this.todos()}`));
  }

  addTodo() {
    const todo = this.newTodo().trim();
    if (todo) {
      this.todos.set([...this.todos(), todo]);
      this.newTodo.set('');
    }
  }

  removeTodo(index: number) {
    this.todos.set(this.todos().filter((_, i) =&amp;gt; i !== index));
  }

  startEdit(index: number, todo: string) {
    this.editingIndex.set(index);
    this.editTodo.set(todo);
  }

  saveEdit(index: number) {
    const updated = this.editTodo().trim();
    if (updated) {
      const updatedList = [...this.todos()];
      updatedList[index] = updated;
      this.todos.set(updatedList);
    }
    this.cancelEdit();
  }

  cancelEdit() {
    this.editingIndex.set(null);
    this.editTodo.set('');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;addTodo()&lt;/code&gt;: Adds a trimmed todo to the list if not empty, then clears input.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;removeTodo(index)&lt;/code&gt;: Removes the todo at the given index (immutable update).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;startEdit(index, todo)&lt;/code&gt;: Switches to edit mode for that todo, pre-fills input.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;saveEdit(index)&lt;/code&gt;: Saves the edited text if valid, updates the list, exits edit mode.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cancelEdit()&lt;/code&gt;:  Exits edit mode and clears the edit input.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;constructor&lt;/code&gt;: Logs todos whenever they change (debugging).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Angular Signals and JavaScript closures are powerful tools for building reactive, performant, and maintainable applications. Signals provide fine-grained reactivity, reducing the overhead of Angular’s traditional change detection, while closures enable encapsulation and state persistence in a flexible way. By mastering these concepts, developers can create robust Angular applications that are easier to reason about and scale effectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For further exploration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check the Angular Signals documentation for official guidance.&lt;/li&gt;
&lt;li&gt;Experiment with signals in small projects to understand their reactivity model.&lt;/li&gt;
&lt;li&gt;Use closures judiciously to encapsulate logic while keeping memory management in mind.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Mastering Node.js: A Comprehensive Guide with Advanced Examples</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Mon, 11 Aug 2025 14:35:29 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/mastering-nodejs-an-in-depth-guide-with-advanced-examples-4pc9</link>
      <guid>https://forem.com/abanoubkerols/mastering-nodejs-an-in-depth-guide-with-advanced-examples-4pc9</guid>
      <description>&lt;p&gt;Node.js is a powerful, open-source runtime environment that brings JavaScript to server-side development. Built on Chrome’s V8 engine, it enables developers to create fast, scalable applications using an event-driven, non-blocking I/O model. This article dives deep into Node.js, covering its core concepts, advanced features, and practical examples to help you harness its full potential.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Node.js?
&lt;/h2&gt;

&lt;p&gt;Node.js allows JavaScript to run outside the browser, making it a versatile choice for server-side applications. Its single-threaded, asynchronous architecture excels at handling I/O-intensive tasks, such as reading files, querying databases, or serving HTTP requests. With a massive ecosystem powered by the Node Package Manager (NPM), Node.js supports rapid development across various use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous I/O&lt;/strong&gt;: Operations like file reading or network requests don’t block the main thread, enabling high concurrency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event-Driven Architecture&lt;/strong&gt;: An event loop processes tasks, ensuring efficient handling of multiple requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Platform&lt;/strong&gt;: Runs seamlessly on Windows, macOS, Linux, and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NPM Ecosystem&lt;/strong&gt;: Over 2 million packages simplify tasks like routing, database integration, and authentication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Ideal for microservices and real-time applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Node.js Works
&lt;/h2&gt;

&lt;p&gt;Node.js uses a single-threaded event loop to manage asynchronous operations. When a request arrives, it’s added to an event queue. The event loop delegates I/O tasks to a worker thread pool, allowing the main thread to process other requests. Once the task completes, a callback or Promise resolves, delivering the result. This non-blocking model ensures Node.js can handle thousands of concurrent connections with minimal resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Node.js&lt;/strong&gt;&lt;br&gt;
Download Node.js from nodejs.org and install it. Verify with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node -v
npm -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This confirms the versions of Node.js and NPM installed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1: Basic HTTP Server
&lt;/h2&gt;

&lt;p&gt;A simple HTTP server showcases Node.js’s core capabilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const http = require('http');

const server = http.createServer((req, res) =&amp;gt; {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Welcome to Node.js!\n');
});

server.listen(3000, () =&amp;gt; {
  console.log('Server running at http://localhost:3000/');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to Run:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save as &lt;code&gt;server.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;node server.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Visit &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; to see the response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;http&lt;/code&gt;module creates a server.&lt;/li&gt;
&lt;li&gt;The callback handles requests (&lt;code&gt;req&lt;/code&gt;) and sends responses (&lt;code&gt;res&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;listen(3000)&lt;/code&gt; binds the server to port 3000.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 2: Asynchronous File Operations
&lt;/h2&gt;

&lt;p&gt;Node.js shines in asynchronous file handling. Here’s an example using the &lt;code&gt;fs&lt;/code&gt;module with &lt;code&gt;async/await&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs').promises;

async function readAndWriteFile() {
  try {
    const data = await fs.readFile('input.txt', 'utf8');
    console.log('File contents:', data);
    await fs.writeFile('output.txt', data.toUpperCase());
    console.log('File written successfully');
  } catch (err) {
    console.error('Error:', err.message);
  }
}

readAndWriteFile();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;input.txt&lt;/code&gt;with some text.&lt;/li&gt;
&lt;li&gt;Run the script to read&lt;code&gt;input.txt&lt;/code&gt; and write its uppercase contents to &lt;code&gt;output.txt&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fs.promises&lt;/code&gt; provides Promise-based APIs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async/await&lt;/code&gt; simplifies asynchronous code.&lt;/li&gt;
&lt;li&gt;Errors are caught using &lt;code&gt;try/catch&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 3: Building a REST API with Express
&lt;/h2&gt;

&lt;p&gt;Express is a lightweight framework for building APIs. Install it:&lt;br&gt;
&lt;code&gt;npm install express&lt;/code&gt;&lt;br&gt;
Here’s a REST API with CRUD operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

let todos = [
  { id: 1, task: 'Learn Node.js', completed: false },
  { id: 2, task: 'Build an API', completed: false }
];

// Get all todos
app.get('/todos', (req, res) =&amp;gt; {
  res.json(todos);
});

// Get a single todo
app.get('/todos/:id', (req, res) =&amp;gt; {
  const todo = todos.find(t =&amp;gt; t.id === parseInt(req.params.id));
  if (!todo) return res.status(404).json({ error: 'Todo not found' });
  res.json(todo);
});

// Create a todo
app.post('/todos', (req, res) =&amp;gt; {
  const todo = { id: todos.length + 1, task: req.body.task, completed: false };
  todos.push(todo);
  res.status(201).json(todo);
});

// Update a todo
app.put('/todos/:id', (req, res) =&amp;gt; {
  const todo = todos.find(t =&amp;gt; t.id === parseInt(req.params.id));
  if (!todo) return res.status(404).json({ error: 'Todo not found' });
  todo.task = req.body.task || todo.task;
  todo.completed = req.body.completed ?? todo.completed;
  res.json(todo);
});

// Delete a todo
app.delete('/todos/:id', (req, res) =&amp;gt; {
  todos = todos.filter(t =&amp;gt; t.id !== parseInt(req.params.id));
  res.status(204).end();
});

app.listen(port, () =&amp;gt; {
  console.log(`API running at http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save as api.js and run node api.js.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use Postman or curl to test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GET &lt;a href="http://localhost:3000/todos" rel="noopener noreferrer"&gt;http://localhost:3000/todos&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;POST &lt;a href="http://localhost:3000/todos" rel="noopener noreferrer"&gt;http://localhost:3000/todos&lt;/a&gt; with &lt;code&gt;{ "task": "Test Node.js" }
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;PUT &lt;a href="http://localhost:3000/todos/1" rel="noopener noreferrer"&gt;http://localhost:3000/todos/1&lt;/a&gt; with &lt;code&gt;{ "task": "Updated task", "completed": true }
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;DELETE &lt;a href="http://localhost:3000/todos/1" rel="noopener noreferrer"&gt;http://localhost:3000/todos/1&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;express.json()&lt;/code&gt; parses JSON payloads.&lt;/li&gt;
&lt;li&gt;Routes handle CRUD operations for a &lt;code&gt;todos&lt;/code&gt;array.&lt;/li&gt;
&lt;li&gt;Error handling ensures invalid requests return appropriate responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 4: Working with Streams
&lt;/h2&gt;

&lt;p&gt;Streams handle large data efficiently by processing it in chunks. Here’s an example of streaming a large file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
const http = require('http');

const server = http.createServer((req, res) =&amp;gt; {
  const stream = fs.createReadStream('large-file.txt');
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  stream.pipe(res);
});

server.listen(3000, () =&amp;gt; {
  console.log('Server running at http://localhost:3000/');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Steps&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a large &lt;code&gt;large-file.txt&lt;/code&gt;(e.g., with repeated text).&lt;/li&gt;
&lt;li&gt;Run the script and visit &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;f&lt;code&gt;s.createReadStream&lt;/code&gt;reads the file in chunks.
-&lt;code&gt;pipe(res)&lt;/code&gt; streams the data directly to the response, reducing memory usage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 5: Custom Middleware in Express
&lt;/h2&gt;

&lt;p&gt;Middleware functions process requests before they reach route handlers. Here’s an example of logging middleware:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const port = 3000;

// Custom middleware to log requests
const logger = (req, res, next) =&amp;gt; {
  console.log(`${req.method} ${req.url} - ${new Date().toISOString()}`);
  next();
};

app.use(logger);
app.use(express.json());

app.get('/', (req, res) =&amp;gt; {
  res.json({ message: 'Welcome to the API' });
});

app.listen(port, () =&amp;gt; {
  console.log(`Server running at http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;logger&lt;/code&gt;middleware logs the method, URL, and timestamp for each request.
-&lt;code&gt;next()&lt;/code&gt; passes control to the next middleware or route.&lt;/li&gt;
&lt;li&gt;Run the script and make requests to see logs in the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 6: Error Handling Middleware
&lt;/h2&gt;

&lt;p&gt;Proper error handling is critical for robust APIs. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

// Route that throws an error
app.get('/error', (req, res, next) =&amp;gt; {
  const err = new Error('Something went wrong!');
  err.status = 500;
  next(err);
});

// Error-handling middleware
app.use((err, req, res, next) =&amp;gt; {
  res.status(err.status || 500).json({
    error: {
      message: err.message,
      status: err.status || 500
    }
  });
});

app.listen(port, () =&amp;gt; {
  console.log(`Server running at http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;/error&lt;/code&gt; route simulates an error.&lt;/li&gt;
&lt;li&gt;The error-handling middleware catches errors and returns a JSON response.&lt;/li&gt;
&lt;li&gt;Test by visiting &lt;a href="http://localhost:3000/error" rel="noopener noreferrer"&gt;http://localhost:3000/error&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 7: Real-Time Chat with Socket.IO
&lt;/h2&gt;

&lt;p&gt;Node.js is ideal for real-time applications using WebSockets. Install &lt;code&gt;socket.io:&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm install socket.io&lt;/code&gt;&lt;br&gt;
Here’s a simple chat server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const http = require('http');
const { Server } = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = new Server(server);
const port = 3000;

app.get('/', (req, res) =&amp;gt; {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) =&amp;gt; {
  console.log('User connected');
  socket.on('chat message', (msg) =&amp;gt; {
    io.emit('chat message', msg); // Broadcast to all clients
  });
  socket.on('disconnect', () =&amp;gt; {
    console.log('User disconnected');
  });
});

server.listen(port, () =&amp;gt; {
  console.log(`Server running at http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HTML (&lt;code&gt;index.html&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;Node.js Chat&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;ul id="messages"&amp;gt;&amp;lt;/ul&amp;gt;
  &amp;lt;form id="form"&amp;gt;
    &amp;lt;input id="input" autocomplete="off" /&amp;gt;&amp;lt;button&amp;gt;Send&amp;lt;/button&amp;gt;
  &amp;lt;/form&amp;gt;
  &amp;lt;script src="/socket.io/socket.io.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script&amp;gt;
    const socket = io();
    const form = document.getElementById('form');
    const input = document.getElementById('input');
    const messages = document.getElementById('messages');

    form.addEventListener('submit', (e) =&amp;gt; {
      e.preventDefault();
      if (input.value) {
        socket.emit('chat message', input.value);
        input.value = '';
      }
    });

    socket.on('chat message', (msg) =&amp;gt; {
      const item = document.createElement('li');
      item.textContent = msg;
      messages.appendChild(item);
    });
  &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save the server code as&lt;code&gt;chat.js&lt;/code&gt; and the HTML as &lt;code&gt;index.html&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run&lt;code&gt;node chat.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Open multiple browser tabs at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; and send messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;socket.io&lt;/code&gt; enables real-time communication.&lt;/li&gt;
&lt;li&gt;The server broadcasts messages to all connected clients.&lt;/li&gt;
&lt;li&gt;The client-side script handles sending and receiving messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advanced Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Microservices: Node.js’s lightweight nature makes it perfect for building microservices, often used with Docker and Kubernetes.&lt;/li&gt;
&lt;li&gt;Serverless Functions: Deploy Node.js functions on platforms like AWS Lambda or Vercel.&lt;/li&gt;
&lt;li&gt;Streaming Media: Use streams for video or audio processing.&lt;/li&gt;
&lt;li&gt;IoT Applications: Handle real-time data from IoT devices with low latency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Performance Optimization Tips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Clustering: Leverage the &lt;code&gt;cluster&lt;/code&gt; module to utilize multiple CPU cores.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i &amp;lt; numCPUs; i++) {
    cluster.fork();
  }
} else {
  http.createServer((req, res) =&amp;gt; {
    res.writeHead(200);
    res.end('Hello from worker');
  }).listen(3000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Caching: Use in-memory stores like Redis for frequently accessed data.&lt;/li&gt;
&lt;li&gt;Profiling: Use tools like &lt;code&gt;clinic&lt;/code&gt;.js to identify bottlenecks.&lt;/li&gt;
&lt;li&gt;Compression: Enable gzip compression in Express with &lt;code&gt;compression&lt;/code&gt;middleware.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advantages and Limitations
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High performance for I/O-bound tasks.&lt;/li&gt;
&lt;li&gt;Unified JavaScript stack for front-end and back-end.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vibrant community and NPM ecosystem.&lt;br&gt;
&lt;strong&gt;Limitations:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Weak for CPU-intensive tasks (e.g., image processing); consider offloading to workers or other languages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Requires careful management to avoid callback hell; use Promises or &lt;code&gt;async/await&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Security Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sanitize Inputs&lt;/strong&gt;: Use libraries like &lt;code&gt;express-validator&lt;/code&gt; to prevent injection attacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use HTTPS&lt;/strong&gt;: Secure traffic with SSL/TLS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limit Requests&lt;/strong&gt;: Implement rate-limiting with &lt;code&gt;express-rate-limit&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep Dependencies Updated&lt;/strong&gt;: Regularly update packages to patch vulnerabilities (&lt;code&gt;npm audit&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Node.js is a versatile runtime for building everything from simple APIs to complex real-time applications. Its asynchronous model, combined with a rich ecosystem, makes it a favorite among developers. By mastering concepts like streams, middleware, and WebSockets, you can unlock Node.js’s full potential. Explore the Node.js documentation and experiment with libraries like mongoose, puppeteer, or jest to build robust, modern applications.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Angular AOT vs JIT: The Complete Guide with Practical Example (v20)</title>
      <dc:creator>Abanoub Kerols</dc:creator>
      <pubDate>Thu, 07 Aug 2025 16:47:23 +0000</pubDate>
      <link>https://forem.com/abanoubkerols/angular-aot-vs-jit-the-complete-guide-with-practical-example-v20-g9p</link>
      <guid>https://forem.com/abanoubkerols/angular-aot-vs-jit-the-complete-guide-with-practical-example-v20-g9p</guid>
      <description>&lt;p&gt;In modern Angular (v20+), understanding the difference between AOT (Ahead-of-Time) and JIT (Just-in-Time) compilation can help developers significantly optimize performance, debugging, and build time. In this article, we’ll explain the difference, how they work, when to use each, and how&lt;code&gt;enableProdMode()&lt;/code&gt; ties into this process.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔍 What Is JIT (Just-in-Time)?
&lt;/h2&gt;

&lt;p&gt;JIT is the default compilation mode when running Angular in development using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng serve&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Angular compiles templates in the browser at runtime.&lt;/li&gt;
&lt;li&gt;Ideal for development: you get faster builds and live reload.&lt;/li&gt;
&lt;li&gt;Debugging tools and developer warnings (like change detection issues) are enabled.&lt;/li&gt;
&lt;li&gt;File size is larger, and performance is slower than AOT.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🚀 What Is AOT (Ahead-of-Time)?
&lt;/h2&gt;

&lt;p&gt;AOT compiles your Angular code at build time using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng build --configuration=production&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Angular compiles templates into optimized JavaScript before shipping to the browser.&lt;/li&gt;
&lt;li&gt;You get faster runtime performance.&lt;/li&gt;
&lt;li&gt;Compiler is not shipped to the browser (more secure and smaller bundle).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;enableProdMode()&lt;/code&gt;is triggered automatically.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🧠 Role of &lt;code&gt;enableProdMode()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The method &lt;code&gt;enableProdMode()&lt;/code&gt; is part of Angular core. When activated, it disables Angular's development-specific checks and tools (like debug data or console warnings).&lt;/p&gt;

&lt;p&gt;In main.ts, it usually looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Angular CLI calls this automatically in production builds.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚙️ Example Using Angular v20 (Standalone Component)
&lt;/h2&gt;

&lt;p&gt;Here’s a simple example to show AOT +&lt;code&gt;enableProdMode()&lt;/code&gt; in action:&lt;/p&gt;

&lt;p&gt;main.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

bootstrapApplication(AppComponent)
  .catch(err =&amp;gt; console.error(err));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;app.component.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    &amp;lt;h1&amp;gt;Hello Angular v20 🚀&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;Counter: {{ counter() }}&amp;lt;/p&amp;gt;
    &amp;lt;button (click)="increment()"&amp;gt;Increment&amp;lt;/button&amp;gt;
  `
})
export class AppComponent {
  counter = signal(0);

  constructor() {
    if (typeof ngDevMode !== 'undefined' &amp;amp;&amp;amp; ngDevMode) {
      console.log('🔧 Development Mode');
    } else {
      console.log('✅ Production Mode');
    }
  }

  increment() {
    this.counter.update(v =&amp;gt; v + 1);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧪 Where Are AOT and JIT Files Located?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Feature                 JIT                         AOT
______________       ______________             ______________
Compilation     | In browser (runtime)     During ng build (buildtime)
Output in /dist |  ❌ No permanent files    Compiled JS is in /dist
Compiler Shipped|        ✅Yes                     ❌ No
File size       |         Bigger                  Smaller
Startup Speed   |         Slower                  Faster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Check AOT code in /dist/main.js:&lt;/strong&gt;&lt;br&gt;
You’ll see code like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ɵɵelementStart(0, "h1");
ɵɵtext(1, "Hello");
ɵɵelementEnd();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✅ Conclusion: Which Should You Use?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; use Case                Recommendation
______________           ______________             
Development      |      Use JIT (ng serve)     
Production       |      Use AOT (ng build --prod)  
Server-side apps |      Use AOT                      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want better performance? ✅ Use AOT + &lt;code&gt;enableProdMode()&lt;/code&gt; in production.&lt;br&gt;
Need fast debugging? 🛠️ Use JIT during development.         &lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
