<?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>DEV Community: Piyush Kumar</title>
    <description>The latest articles on DEV Community by Piyush Kumar (@dottt).</description>
    <link>https://dev.to/dottt</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%2F1041618%2Fc38d26ed-a764-419c-b026-e4bbfe472d07.jpeg</url>
      <title>DEV Community: Piyush Kumar</title>
      <link>https://dev.to/dottt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dottt"/>
    <language>en</language>
    <item>
      <title>Push Notifications in React Native + Node.js: The Complete Setup Guide (Part 1)</title>
      <dc:creator>Piyush Kumar</dc:creator>
      <pubDate>Sat, 21 Mar 2026 18:42:44 +0000</pubDate>
      <link>https://dev.to/dottt/push-notifications-in-react-native-nodejs-the-complete-setup-guide-part-1-2673</link>
      <guid>https://dev.to/dottt/push-notifications-in-react-native-nodejs-the-complete-setup-guide-part-1-2673</guid>
      <description>&lt;p&gt;&lt;em&gt;A developer's guide to Firebase Cloud Messaging — from zero to production in under 3 minutes&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What is FCM and Why Should You Care?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Firebase Cloud Messaging (FCM)&lt;/strong&gt; is Google's free service for sending push notifications to Android and iOS devices. The best part? It's &lt;strong&gt;completely free&lt;/strong&gt; — unlimited messages, no credit card required, ever.&lt;/p&gt;

&lt;p&gt;Before we dive in, here's why you shouldn't build your own notification system:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Your DIY Solution&lt;/th&gt;
&lt;th&gt;With FCM&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;❌ Can't deliver when app is closed&lt;/td&gt;
&lt;td&gt;✅ OS handles delivery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;❌ Drains battery with persistent connections&lt;/td&gt;
&lt;td&gt;✅ Uses OS push channel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;❌ Complex iOS APNs setup&lt;/td&gt;
&lt;td&gt;✅ FCM abstracts it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;❌ Need to maintain infrastructure&lt;/td&gt;
&lt;td&gt;✅ Google's global network&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  How It Works: The 30-Second Explanation
&lt;/h2&gt;

&lt;p&gt;Your backend never talks directly to users' phones. Instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your Backend  →  FCM Servers (Google)  →  User's Device
   (sends)          (routes)                (receives)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;FCM uses a &lt;strong&gt;device token&lt;/strong&gt; — a unique string representing one app install on one device — to route messages to the right phone.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Architecture
&lt;/h2&gt;

&lt;p&gt;Here's what you're building:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────────────────┐
│                   Your Firebase Project                  │
│                                                           │
│  ┌──────────────────┐      ┌──────────────────────┐     │
│  │  Frontend (App)   │      │   Backend (Node.js)   │     │
│  │  React Native     │      │   Express + Database  │     │
│  │                   │      │                       │     │
│  │  Uses:            │      │  Uses:                │     │
│  │  • google-        │      │  • Service Account    │     │
│  │    services.json  │      │    JSON (in .env)     │     │
│  │  • @react-native- │      │  • firebase-admin     │     │
│  │    firebase/      │      │    SDK                │     │
│  │    messaging      │      │                       │     │
│  │                   │      │                       │     │
│  │  Job:             │      │  Job:                 │     │
│  │  • Get token      │      │  • Send push to       │     │
│  │  • Request        │      │    token via FCM      │     │
│  │    permission     │      │    API                │     │
│  │  • Receive push   │      │                       │     │
│  └──────────────────┘      └──────────────────────┘     │
└─────────────────────────────────────────────────────────┘
                   ↓                    ↓
            ┌──────────────────────────────┐
            │      FCM Servers (Google)     │
            │   Routes to correct device    │
            └──────────────────────────────┘
                          ↓
                    User's Phone
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 1: Firebase Console Setup (5 minutes)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create Your Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;a href="https://console.firebase.google.com" rel="noopener noreferrer"&gt;console.firebase.google.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;"Add project"&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Give it a name (e.g., &lt;code&gt;my-awesome-app&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Add Android App
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Inside your project → &lt;strong&gt;Add app → Android&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Enter your package name (e.g., &lt;code&gt;com.myapp&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Download &lt;code&gt;google-services.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Place it at &lt;code&gt;android/app/google-services.json&lt;/code&gt; in your React Native project&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Get Backend Credentials
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Project Settings → Service Accounts&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;"Generate new private key"&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Downloads a JSON file — this is your admin credential&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;⚠️ &lt;strong&gt;Security Alert:&lt;/strong&gt; Never commit this file to git. Store it as an environment variable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Backend Setup (Node.js)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install Firebase Admin SDK
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;firebase-admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Initialize Firebase
&lt;/h3&gt;

&lt;p&gt;Create &lt;code&gt;config/firebase.js&lt;/code&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;admin&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="s1"&gt;firebase-admin&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;serviceAccount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FIREBASE_SERVICE_ACCOUNT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initializeApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;credential&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;credential&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;serviceAccount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Push Notification Service
&lt;/h3&gt;

&lt;p&gt;Create &lt;code&gt;services/pushNotification.js&lt;/code&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;admin&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="s1"&gt;../config/firebase&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sendPushNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deviceToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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;message&lt;/span&gt; &lt;span class="o"&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="nx"&gt;deviceToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// Custom key-value pairs&lt;/span&gt;
            &lt;span class="na"&gt;android&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;high&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;  &lt;span class="c1"&gt;// Wakes up the device&lt;/span&gt;
            &lt;span class="p"&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;messaging&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;message&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="s1"&gt;Push sent successfully:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&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;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;Error sending push:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Handle expired/invalid tokens&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;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;messaging/registration-token-not-registered&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="c1"&gt;// Remove token from database&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="s1"&gt;Token is invalid, should be removed from DB&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="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&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;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;sendPushNotification&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Environment Variables
&lt;/h3&gt;

&lt;p&gt;Create/update your &lt;code&gt;.env&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Paste the entire service account JSON as a single-line string&lt;/span&gt;
&lt;span class="nv"&gt;FIREBASE_SERVICE_ACCOUNT&lt;/span&gt;&lt;span class="o"&gt;={&lt;/span&gt;&lt;span class="s2"&gt;"type"&lt;/span&gt;:&lt;span class="s2"&gt;"service_account"&lt;/span&gt;,&lt;span class="s2"&gt;"project_id"&lt;/span&gt;:&lt;span class="s2"&gt;"your-project-id"&lt;/span&gt;,...&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 3: Database Setup
&lt;/h2&gt;

&lt;p&gt;Add a column to store device tokens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- For PostgreSQL&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;fcm_token&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- For MySQL&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;fcm_token&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&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;Important:&lt;/strong&gt; If you need multi-device support (same user, multiple phones), create a separate table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;device_tokens&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;fcm_token&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;device_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="n"&gt;NOW&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fcm_token&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;
  
  
  Step 4: API Endpoint to Save Token
&lt;/h2&gt;

&lt;p&gt;Create an endpoint to receive tokens from your app:&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="c1"&gt;// routes/user.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&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="s1"&gt;express&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;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Assuming you have authentication middleware&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/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;authenticateUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;try&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;fcm_token&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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;userId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&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="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// From auth middleware&lt;/span&gt;

        &lt;span class="c1"&gt;// Update user's FCM token&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UPDATE users SET fcm_token = $1 WHERE id = $2&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;fcm_token&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="p"&gt;);&lt;/span&gt;

        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;success&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;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Token saved successfully&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="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&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;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;Error saving FCM 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;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&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;Failed to save token&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="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Understanding Device Tokens
&lt;/h2&gt;

&lt;p&gt;A device token 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;eExKj8mTR4pQ:APA91bH...very-long-alphanumeric-string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Facts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unique per device + per app install&lt;/li&gt;
&lt;li&gt;Changes when: app is reinstalled, device is reset, app data is cleared&lt;/li&gt;
&lt;li&gt;Same device, same app, but reinstalled = &lt;strong&gt;new token&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Token Lifecycle
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;th&gt;Token Changes?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;App update&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;App uninstall + reinstall&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User clears app data&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Device factory reset&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User switches phones&lt;/td&gt;
&lt;td&gt;✅ Yes (new device)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  The Complete Flow Diagram
&lt;/h2&gt;

&lt;p&gt;Here's how everything connects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────────────────────┐
│                    REGISTRATION FLOW                         │
└─────────────────────────────────────────────────────────────┘

[User's Phone]              [Your Backend]          [Database]
      │                           │                      │
      │─── App opens ────────────►│                      │
      │    Firebase SDK runs      │                      │
      │    Generates token        │                      │
      │                           │                      │
      │─── Login/Signup ─────────►│                      │
      │                           │                      │
      │─── Send FCM token ───────►│                      │
      │    PUT /api/user/token    │                      │
      │                           │                      │
      │                           │─── Save token ──────►│
      │                           │    UPDATE users      │
      │                           │    SET fcm_token     │
      │                           │                      │
      │◄─── Success response ─────│                      │


┌─────────────────────────────────────────────────────────────┐
│                   NOTIFICATION FLOW                          │
└─────────────────────────────────────────────────────────────┘

[Your Backend]          [Database]        [FCM]         [User's Phone]
      │                      │              │                  │
      │─── Event occurs ────►│              │                  │
      │    (new message,     │              │                  │
      │     order update,    │              │                  │
      │     etc.)            │              │                  │
      │                      │              │                  │
      │─── Get user token ──►│              │                  │
      │    SELECT fcm_token  │              │                  │
      │                      │              │                  │
      │◄─── Return token ────│              │                  │
      │                      │              │                  │
      │─── Send push ───────────────────────►│                  │
      │    admin.messaging()                │                  │
      │    .send({ token,                   │                  │
      │            title,                    │                  │
      │            body })                   │                  │
      │                                      │                  │
      │                                      │─── Route to ────►│
      │                                      │    device        │
      │                                      │                  │
      │                                      │                  │
      │                                      │   [Notification  │
      │                                      │    appears on    │
      │                                      │    lock screen]  │
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What's in Part 2?
&lt;/h2&gt;

&lt;p&gt;In the next article, we'll cover:&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Frontend implementation&lt;/strong&gt; — Getting the token in React Native&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Handling notifications&lt;/strong&gt; — When app is open, background, or closed&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Token refresh logic&lt;/strong&gt; — Keeping tokens up-to-date&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Testing strategies&lt;/strong&gt; — How to test notifications locally&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Production checklist&lt;/strong&gt; — Everything you need before going live&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Reference: Is FCM Really Free?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Service&lt;/th&gt;
&lt;th&gt;Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;FCM push notifications&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;✅ Free (unlimited)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Firebase project (Spark plan)&lt;/td&gt;
&lt;td&gt;✅ Free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Firebase Admin SDK&lt;/td&gt;
&lt;td&gt;✅ Free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;React Native Firebase package&lt;/td&gt;
&lt;td&gt;✅ Free (open source)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your server hosting&lt;/td&gt;
&lt;td&gt;💰 You pay for hosting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your database&lt;/td&gt;
&lt;td&gt;💰 You pay for database&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;FCM has been free since Google acquired Firebase in 2014. No message limits, ever.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes to Avoid
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ❌ Committing service account JSON to Git
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Add to &lt;code&gt;.gitignore&lt;/code&gt;, use environment variables&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Not handling token expiration
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Catch &lt;code&gt;messaging/registration-token-not-registered&lt;/code&gt; errors and remove stale tokens&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Forgetting permission requests
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Always request notification permission on Android 13+ and iOS&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Testing only with app open
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Test all three states: foreground, background, and killed&lt;/p&gt;




&lt;h2&gt;
  
  
  Ready to Implement?
&lt;/h2&gt;

&lt;p&gt;You now have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firebase project configured&lt;/li&gt;
&lt;li&gt;Backend initialized with admin SDK&lt;/li&gt;
&lt;li&gt;Database ready to store tokens&lt;/li&gt;
&lt;li&gt;API endpoint to receive tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Next step:&lt;/strong&gt; Part 2 will show you the React Native frontend code to complete the integration.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Series Navigation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Part 1: Concepts &amp;amp; Setup&lt;/strong&gt; &lt;em&gt;(you are here)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Part 2: Frontend Implementation &amp;amp; Testing &lt;em&gt;(coming soon)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Found this helpful?
&lt;/h3&gt;

&lt;p&gt;Follow me for Part 2, where we'll implement the complete React Native frontend and handle all notification scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tags:&lt;/strong&gt; #react-native #nodejs #fcm #push-notifications #firebase #mobile-development&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>fcm</category>
      <category>node</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Top 10 technologies/framework to learn as a MERN stack developer in 2023</title>
      <dc:creator>Piyush Kumar</dc:creator>
      <pubDate>Sat, 11 Mar 2023 16:02:50 +0000</pubDate>
      <link>https://dev.to/dottt/top-10-technologiesframework-to-learn-as-a-mern-stack-developer-in-2023-144o</link>
      <guid>https://dev.to/dottt/top-10-technologiesframework-to-learn-as-a-mern-stack-developer-in-2023-144o</guid>
      <description>&lt;p&gt;As a MERN (MongoDB, Express, React, and Node.js) stack developer, the &lt;strong&gt;top 10 technologies&lt;/strong&gt; and frameworks worth considering to learn in 2023:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. GraphQL:&lt;/strong&gt; It is a query language for APIs that enables developers to get exactly the data they need in a single request. It can be a useful skill to have as a MERN stack developer, especially for building scalable and efficient APIs. &lt;a href="https://graphql.org/code/" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Next.js:&lt;/strong&gt; Next.js is a popular React framework that offers server-side rendering, static site generation, and other advanced features. It can help improve the performance and user experience of your React applications. &lt;a href="https://vercel.com/docs" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Docker:&lt;/strong&gt; Docker is a containerization platform that enables developers to package and deploy applications as containers. It can be a useful tool for managing dependencies and simplifying the deployment process. &lt;a href="https://docs.docker.com/" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Redux:&lt;/strong&gt; Redux is a predictable state container for JavaScript apps that can help manage the state of your React applications. It can be a valuable skill to have as a MERN stack developer, especially for building complex applications. &lt;a href="https://redux.js.org/" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. TypeScript:&lt;/strong&gt; TypeScript is a typed superset of JavaScript that can help improve the maintainability and scalability of your code. It can be a useful skill to have as a MERN stack developer, especially for building large-scale applications. &lt;a href="https://www.typescriptlang.org/docs/" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. AWS Lambda:&lt;/strong&gt; AWS Lambda is a serverless compute service that enables developers to run code without managing servers. It can be a useful tool for building scalable and cost-effective backend services. &lt;a href="https://docs.aws.amazon.com/lambda/index.html" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. React Native:&lt;/strong&gt; React Native is a popular framework for building mobile applications using React. It can be a valuable skill to have as a MERN stack developer, especially if you're interested in building mobile applications. &lt;a href="https://reactnative.dev/docs/components-and-apis" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Socket.io:&lt;/strong&gt; Socket.io is a library that enables real-time, bidirectional communication between clients and servers. It can be a useful tool for building real-time applications, such as chat applications. &lt;a href="https://socket.io/docs/v4/" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Serverless Framework:&lt;/strong&gt; Serverless Framework is a framework that enables developers to build and deploy serverless applications easily. It can be a useful tool for building scalable and cost-effective backend services. &lt;a href="https://www.serverless.com/framework/docs" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Jest:&lt;/strong&gt; Jest is a popular testing framework for JavaScript applications. It can help ensure the quality and reliability of your code, and can be a valuable skill to have as a MERN stack developer. &lt;a href="https://jestjs.io/docs/getting-started" rel="noopener noreferrer"&gt;You can learn more about this here...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mern</category>
      <category>libraries</category>
      <category>framework</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Sorting Algorithm with Python Code</title>
      <dc:creator>Piyush Kumar</dc:creator>
      <pubDate>Thu, 09 Mar 2023 05:30:03 +0000</pubDate>
      <link>https://dev.to/dottt/sorting-algorithm-with-python-code-ilo</link>
      <guid>https://dev.to/dottt/sorting-algorithm-with-python-code-ilo</guid>
      <description>&lt;p&gt;&lt;strong&gt;Sorting algorithms&lt;/strong&gt; are a fundamental part of computer science and are used to arrange a list of elements in a specific order. There are numerous sorting algorithms available, each with its own set of strengths and weaknesses. In this blog, we will discuss the most common sorting algorithms and their implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Bubble Sort:&lt;/strong&gt;&lt;br&gt;
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The algorithm gets its name from the way smaller elements "bubble" to the top of the list.&lt;/p&gt;

&lt;p&gt;Here's an implementation of Bubble Sort in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bubbleSort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] &amp;gt; arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Selection Sort:&lt;/strong&gt;&lt;br&gt;
Selection sort is an algorithm that sorts an array by repeatedly finding the minimum element from the unsorted part of the array and putting it at the beginning. It has a complexity of O(n^2).&lt;/p&gt;

&lt;p&gt;Here's an implementation of Selection Sort in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def selectionSort(arr):
    n = len(arr)
    for i in range(n):
        min_idx = i
        for j in range(i+1, n):
            if arr[min_idx] &amp;gt; arr[j]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Insertion Sort:&lt;/strong&gt;&lt;br&gt;
Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. It has a complexity of O(n^2).&lt;/p&gt;

&lt;p&gt;Here's an implementation of Insertion Sort in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def insertionSort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j &amp;gt;= 0 and key &amp;lt; arr[j] :
                arr[j + 1] = arr[j]
                j -= 1
        arr[j + 1] = key

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Quick Sort:&lt;/strong&gt;&lt;br&gt;
Quick sort is a divide and conquer algorithm that picks an element as a pivot and partitions the array around the pivot, putting all elements smaller than the pivot to its left and all elements greater than the pivot to its right. It then recursively sorts the two subarrays. It has a complexity of O(nlogn).&lt;/p&gt;

&lt;p&gt;Here's an implementation of Quick Sort in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def partition(arr, low, high):
    i = (low-1)         
    pivot = arr[high]     

    for j in range(low, high):
        if arr[j] &amp;lt;= pivot:
            i = i+1
            arr[i], arr[j] = arr[j], arr[i]

    arr[i+1], arr[high] = arr[high], arr[i+1]
    return (i+1)

def quickSort(arr, low, high):
    if low &amp;lt; high:
        pi = partition(arr, low, high)
        quickSort(arr, low, pi-1)
        quickSort(arr, pi+1, high)

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Merge Sort:&lt;/strong&gt;&lt;br&gt;
Merge sort is a divide and conquer algorithm that divides the array into two halves, recursively sorts the two halves, and then merges the sorted halves. It has a complexity of O(nlogn).&lt;/p&gt;

&lt;p&gt;Here's an implementation of Merge Sort in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def merge_sort(arr):
    if len(arr) &amp;gt; 1:
        mid = len(arr) // 2
        left_half = arr[:mid]
        right_half = arr[mid:]

        # Recursive calls to divide the array into smaller subarrays
        merge_sort(left_half)
        merge_sort(right_half)

        i = j = k = 0

        # Merge the subarrays
        while i &amp;lt; len(left_half) and j &amp;lt; len(right_half):
            if left_half[i] &amp;lt; right_half[j]:
                arr[k] = left_half[i]
                i += 1
            else:
                arr[k] = right_half[j]
                j += 1
            k += 1

        while i &amp;lt; len(left_half):
            arr[k] = left_half[i]
            i += 1
            k += 1

        while j &amp;lt; len(right_half):
            arr[k] = right_half[j]
            j += 1
            k += 1

# Example usage
arr = [6, 5, 3, 1, 8, 7, 2, 4]
merge_sort(arr)
print(arr)  # Output: [1, 2, 3, 4, 5, 6, 7, 8]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Heap Sort:&lt;/strong&gt;&lt;br&gt;
Heap sort is a comparison-based sorting algorithm that works by first building a binary heap from the array to be sorted, and then repeatedly extracting the maximum element from the heap and putting it at the end of the array until the heap is empty. The heap is built in such a way that the root node of each subtree has a larger value than its children (in a max-heap), or a smaller value (in a min-heap).&lt;/p&gt;

&lt;p&gt;The steps to perform heap sort are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build a max heap from the input array.&lt;/li&gt;
&lt;li&gt;Extract the maximum element from the heap (which is always the root element) and swap it with the last element of the heap.&lt;/li&gt;
&lt;li&gt;Reduce the heap size by one and heapify the root of the heap (which is now the last element of the heap).&lt;/li&gt;
&lt;li&gt;Repeat steps 2 and 3 until the heap is empty.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's the algorithm in more detail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def heapify(arr, n, i):
    """
    A helper function that heapifies a subtree rooted with node i
    in a given array arr of size n.
    """
    largest = i
    l = 2 * i + 1
    r = 2 * i + 2

    # If left child is larger than root
    if l &amp;lt; n and arr[l] &amp;gt; arr[largest]:
        largest = l

    # If right child is larger than largest so far
    if r &amp;lt; n and arr[r] &amp;gt; arr[largest]:
        largest = r

    # If largest is not root
    if largest != i:
        arr[i], arr[largest] = arr[largest], arr[i]  # Swap root with largest child

        # Recursively heapify the affected sub-tree
        heapify(arr, n, largest)

def heap_sort(arr):
    """
    A function to perform Heap Sort on a given array arr.
    """
    n = len(arr)

    # Build a max-heap
    for i in range(n // 2 - 1, -1, -1):
        heapify(arr, n, i)

    # Extract elements one by one
    for i in range(n - 1, 0, -1):
        arr[0], arr[i] = arr[i], arr[0]  # Swap the root with the last element
        heapify(arr, i, 0)

    return arr

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

&lt;/div&gt;



&lt;p&gt;You can use this function to sort a list by calling heap_sort(arr) where arr is the list to be sorted.&lt;/p&gt;

</description>
      <category>sorting</category>
      <category>sort</category>
      <category>python</category>
      <category>code</category>
    </item>
  </channel>
</rss>
