<?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: Nirvan Jha</title>
    <description>The latest articles on DEV Community by Nirvan Jha (@nirvanjha2004).</description>
    <link>https://dev.to/nirvanjha2004</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%2F1630834%2F49a83a36-3050-447e-a82a-e2bd0ec5c7dc.jpeg</url>
      <title>DEV Community: Nirvan Jha</title>
      <link>https://dev.to/nirvanjha2004</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nirvanjha2004"/>
    <language>en</language>
    <item>
      <title>WebSocket : Create your First WebSocket connection</title>
      <dc:creator>Nirvan Jha</dc:creator>
      <pubDate>Wed, 26 Jun 2024 15:32:14 +0000</pubDate>
      <link>https://dev.to/nirvanjha2004/websocket-create-your-first-websocket-connection-47o2</link>
      <guid>https://dev.to/nirvanjha2004/websocket-create-your-first-websocket-connection-47o2</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Backend Communication?&lt;/strong&gt;&lt;br&gt;
When One Backend system interacts with other Backend systems, Its called backend communication. In real world, there are various backend systems not just one. Lets see some examples:- &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For example, for a website like PayTM, whenever you do a transaction, the following might happen&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20p7hsgb7l2ag3l905fv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20p7hsgb7l2ag3l905fv.png" alt="Image description" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;Backend1&lt;/strong&gt; will be doing the Money Transfer work and will delegate the task of sending notification of money transfer via email, messaging to other backends. &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;For leetcode (Leetcode is a problem solving website for programmers), whenever the user submits a problem, the following might happen - &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d2fgelrd4mamh4p3ukg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d2fgelrd4mamh4p3ukg.png" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of communication:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Synchronous (Strong coupling):-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HTTP (REST/GraphQL)&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Asynchronous (Weak coupling):-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Messaging queues&lt;/li&gt;
&lt;li&gt;Pub subs&lt;/li&gt;
&lt;li&gt;Server-Sent Events &lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;WebSockets&lt;br&gt;
WebSockets are still debatable whether they are sync or async.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So what is Synchronous:- &lt;br&gt;
HTTP is considered synchronous due to its request-response model. In this model, a client sends a request to a server and waits for a response. The client sends a request, waits for a response, and only after receiving the response can it send another request.&lt;/p&gt;

&lt;p&gt;Asynchronous:- &lt;br&gt;
It refers to a model where the client does not wait for the server to respond before proceeding with other tasks. Instead, the client can send multiple requests, and handle responses as they arrive. For Example:- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4nfuhmiuaxvwqrofce6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4nfuhmiuaxvwqrofce6.png" alt="Image description" width="800" height="755"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Websockets
&lt;/h2&gt;

&lt;p&gt;Websockets are a way of communication between the client(typically a web browser) and the server.&lt;br&gt;
So why are we using websockets and why not HTTP??&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In HTTP, network Handshake happens for every request. Here the server 
makes a request and waits for the response and when it recieves the 
response, the connection gets closed. Now to make another request the 
network handshake happens again. See below:- &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl47iu2ivanwt20cagesd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl47iu2ivanwt20cagesd.png" alt="Image description" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No way to push server side events. In an Http connection you can not 
send events or requests (You can not do fetch("URL") from the server 
side) from the server to the client. Only the Client can make requests. 
(You can use polling but not the best approach).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;WebSockets provide a way to establish a persistent, full-duplex communication channel over a single TCP connection between the client (typically a web browser) and the server and solves the above two problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj9d89wwuvy0oulua72dt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj9d89wwuvy0oulua72dt.png" alt="Image description" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Cases for WebSockets:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Real-Time Applications: Chat applications, live sports updates, real-time gaming, and any application requiring instant updates can benefit from WebSockets.&lt;/li&gt;
&lt;li&gt;Live Feeds: Financial tickers, news feeds, and social media updates are examples where WebSockets can be used to push live data to users.&lt;/li&gt;
&lt;li&gt;Interactive Services: Chat application, Collaborative editing tools, live customer support chat, and interactive webinars can use WebSockets to enhance user interaction.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Websockets in Node.js&lt;/strong&gt;&lt;br&gt;
There are various libraries that let you create a ws server:-&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/websocket"&gt;https://www.npmjs.com/package/websocket&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/websockets/ws"&gt;https://github.com/websockets/ws&lt;/a&gt;&lt;br&gt;
&lt;a href="https://socket.io/"&gt;https://socket.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ll be using the ws library today. Lets dive into code:- &lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;Backend&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Ws in Node.js (Code)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize an empty Node.js project
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Add tsconfig to it
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Update tsconfig
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"rootDir": "./src",
"outDir": "./dist",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;3.1. Create a src folder and inside it create index.ts file. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install ws
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i ws @types/ws
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you want to use the standard http library: - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code using http library
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import WebSocket, { WebSocketServer } from 'ws';
import http from 'http';

const server = http.createServer(function(request: any, response: any) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.end("hi there");
});

const wss = new WebSocketServer({ server });

wss.on('connection', function connection(ws) {
  ws.on('error', console.error);

  ws.on('message', function message(data, isBinary) {
    wss.clients.forEach(function each(client) {
      if (client.readyState === WebSocket.OPEN) {
        client.send(data, { binary: isBinary });
      }
    });
  });

  ws.send('Hello! Message From Server!!');
});

server.listen(8080, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you want to use Express Library:-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code using express
5.1. Install Express
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express @types/express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;5.2. Writw this code in index.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 express from 'express'
import { WebSocketServer } from 'ws'

const app = express()
const httpServer = app.listen(8080)

const wss = new WebSocketServer({ server: httpServer });

wss.on('connection', function connection(ws) {
  ws.on('error', console.error);

  ws.on('message', function message(data, isBinary) {
    wss.clients.forEach(function each(client) {
      if (client.readyState === WebSocket.OPEN) {
        client.send(data, { binary: isBinary });
      }
    });
  });

  ws.send('Hello! Message From Server!!');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Open the terminal and write:-
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsc -b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node dist/index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To check whether the websocket is established or not u can go to 
&lt;a href="https://hoppscotch.io/realtime/websocket"&gt;https://hoppscotch.io/realtime/websocket&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwje6yw4u6xfzdu2kr1hw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwje6yw4u6xfzdu2kr1hw.png" alt="Image description" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Client side code&lt;/u&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Websocket  is a browser API that you can access (very similar to fetch).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Create a React project
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a websocket connection to the server in App.tsx
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from 'react'
import './App.css'

function App() {
  const [socket, setSocket] = useState&amp;lt;WebSocket | null&amp;gt;(null);

  useEffect(() =&amp;gt; {
    const newSocket = new WebSocket('ws://localhost:8080');
    newSocket.onopen = () =&amp;gt; {
      console.log('Connection established');
      newSocket.send('Hello Server!');
    }
    newSocket.onmessage = (message) =&amp;gt; {
      console.log('Message received:', message.data);
    }
    setSocket(newSocket);
    return () =&amp;gt; newSocket.close();
  }, [])

//Here you can write the code to maka chat application by adding a input box, by creating a state and the storing the message in it and rendering the messages on the screen. 
  return (
    &amp;lt;&amp;gt;
      hi there
    &amp;lt;/&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In Nextjs write this code in Page.tsx and make it client side by writing "use client" at the top of Page.tsx&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the terminal to run this project:-
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

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

&lt;/div&gt;



&lt;p&gt;And now you are good to go.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>WebRTC : Create Your First WebRTC connection!</title>
      <dc:creator>Nirvan Jha</dc:creator>
      <pubDate>Sun, 23 Jun 2024 14:02:05 +0000</pubDate>
      <link>https://dev.to/nirvanjha2004/webrtc-create-your-first-webrtc-connection-1954</link>
      <guid>https://dev.to/nirvanjha2004/webrtc-create-your-first-webrtc-connection-1954</guid>
      <description>&lt;p&gt;What is WebRTC?&lt;br&gt;
WebRTC is an opensource project that provides webApplications and sites to establish a peer2peer connection and do a  real-time communication. It allows to send video, audio and data sharing between browsers. &lt;/p&gt;

&lt;p&gt;P2P&lt;br&gt;
WebRTC is a peer to peer protocol. This means that you directly send your media over to the other person without the need of a central server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy91zc6d75qsll3rs7np7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy91zc6d75qsll3rs7np7.png" alt="Image description" width="551" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: &lt;br&gt;
You do need a central server for signaling and sometimes for sending media as well (turn). We’ll be discussing this later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Latency:- latency is the time it takes for data to travel from the source to the destination.&lt;/p&gt;

&lt;p&gt;You use WebRTC for applications that require Less latency. &lt;br&gt;
Examples include&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Zoom/Google meet (Multi party call)&lt;/li&gt;
&lt;li&gt;Omegle, teaching (1:1 call)&lt;/li&gt;
&lt;li&gt;30FPS games (WebRTC can also send data)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What is a Signaling Server?&lt;br&gt;
To establish a connection, Both the server need to send each other their address in order to know whom they have to connect to. A signaling server is used for that. A signaling server itself does not handle the actual media or data transfer but facilitates the initial communication setup.&lt;br&gt;
It is usually a WebSocket server but can be a (http) server as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb80m7wxvm14zos79mz0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb80m7wxvm14zos79mz0e.png" alt="Image description" width="548" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is a STUN server?&lt;br&gt;
The primary purpose of a STUN server is to allow a client (or Browser) to determine its public-facing (which the world sees) IP address and the port that the NAT(explained below) has assigned to a communication session. This information is then used in the signaling process to facilitate direct communication between Browsers.&lt;/p&gt;

&lt;p&gt;So what is NAT(Network Address Translation)??&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ff30htuo3dv4s3j021g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ff30htuo3dv4s3j021g.png" alt="Image description" width="705" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simple NAT Example&lt;br&gt;
1.Your laptop (192.168.1.2) wants to visit a website.&lt;br&gt;
2.The router changes the address to its public IP (e.g., 203.0.113.1) and sends the request.&lt;br&gt;
3.The website sends data back to 203.0.113.1.&lt;br&gt;
4.The router receives the data, knows it was for 192.168.1.2, and sends it to your laptop.&lt;/p&gt;

&lt;p&gt;Coming back to STUN server:- This is How it Works&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0igbaaqkkr6kyvfzg9b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0igbaaqkkr6kyvfzg9b.png" alt="Image description" width="583" height="754"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ICE Candidates :=&amp;gt;&lt;/p&gt;

&lt;p&gt;ICE (Interactive Connectivity Establishment) candidates are network endpoints that WebRTC peers use to establish direct peer-to-peer connections. Each ICE candidate consists of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;IP Address: This is the address that identifies a device on a network. It can be either a local (private) IP address or a public IP address.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Port Number: Ports are numeric identifiers used by networking protocols to distinguish different types of traffic on the same IP address. They help direct data to the correct application or service running on a device.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In simple terms ICE candidates are potential IP address and Port through which two Browsers or peers can establish connection.&lt;/p&gt;

&lt;p&gt;If two friends are trying to connect to each other in a hostel wifi , then they can connect via their private router ice candidates. &lt;/p&gt;

&lt;p&gt;If two people from different countries are trying to connect to each other, then they would connect via their public IPs.&lt;/p&gt;

&lt;p&gt;What is a TURN server?&lt;/p&gt;

&lt;p&gt;A lot of times, your network doesn’t allow media to come in from browser2. This depends on how restrictive your network is.&lt;br&gt;
So When direct peer-to-peer connections fail due to NAT traversal issues(or Router issues), a TURN server acts as a solution. It relays media and data between peers, ensuring that communication can still occur even if direct connections are not possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4s3e0xht9q2mop8yi7b6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4s3e0xht9q2mop8yi7b6.png" alt="Image description" width="597" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Offer&lt;/strong&gt;&lt;br&gt;
The process of the first browser (the one initiating connection) sending their ice candidates to the other side.&lt;br&gt;
&lt;strong&gt;Answer&lt;/strong&gt;&lt;br&gt;
The other side returning their ice candidates is called the answer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SDP - Session description protocol&lt;/strong&gt;&lt;br&gt;
A single file that contains all your ice candidates, what media you want to send, what protocols you’ve used to encode the media. This is the file that is sent in the offer and received in the answer.&lt;br&gt;
SDP Look like this:-&lt;br&gt;
(No need to understand this....senior dev things :))&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywsja4n19qvo5l32qzhe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywsja4n19qvo5l32qzhe.png" alt="Image description" width="800" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary:-&lt;/strong&gt;&lt;br&gt;
1.You need a signaling server, stun server to initiate the webrtc conn b/w the parties. You can kill these once the conn is made.&lt;br&gt;
2.You need to include a turn server incase any of the users are on a restrictive network so you can get back a turn ice candidate as well.&lt;/p&gt;

&lt;p&gt;Now Lets Dive into some Coding Stuff:-&lt;/p&gt;

&lt;p&gt;RTCPeerConnection (pc, peer connection)&lt;br&gt;
This is a class that the browser provides you with which gives you access to the SDP, lets you create answers / offers , lets you send media.&lt;br&gt;
This class hides all the complexity of webrtc from the developer.&lt;br&gt;
To know more:- &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection"&gt;https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basic Steps:- (Don't worry if it feels jargony right now!)&lt;br&gt;
Connecting the two sides&lt;/p&gt;

&lt;p&gt;The steps to create a webrtc connection between 2 sides includes -  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser 1 creates an RTCPeerConnection&lt;/li&gt;
&lt;li&gt;Browser 1 creates an offer&lt;/li&gt;
&lt;li&gt;Browser 1 sets the local description to the offer&lt;/li&gt;
&lt;li&gt;Browser 1 sends the offer to the other side through the signaling server&lt;/li&gt;
&lt;li&gt;Browser 2 receives the offer from the signaling server&lt;/li&gt;
&lt;li&gt;Browser 2 sets the remote description to the offer&lt;/li&gt;
&lt;li&gt;Browser 2 creates an answer&lt;/li&gt;
&lt;li&gt;Browser 2 sets the local description to be the answer&lt;/li&gt;
&lt;li&gt;Browser 2 sends the answer to the other side through the signaling server&lt;/li&gt;
&lt;li&gt;Browser 1 receives the answer and sets the remote description&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is just to establish the p2p connection b/w the two parties&lt;/p&gt;

&lt;p&gt;To actually send media, we have to &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ask for camera /mic permissions&lt;/li&gt;
&lt;li&gt;Get the audio and video streams&lt;/li&gt;
&lt;li&gt;Call addTrack on the pc&lt;/li&gt;
&lt;li&gt;This would trigger a OnTrack callback on the other side&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Implementation&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We will be writing the code in Node.js for the Signaling server. It will be a websocket server that supports 3 types of messages

&lt;ol&gt;
&lt;li&gt;createOffer&lt;/li&gt;
&lt;li&gt;createAnswer&lt;/li&gt;
&lt;li&gt;addIceCandidate&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;React + PeerConnectionObject on the frontend.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to get an idea on how it works:-  &lt;a href="https://jsfiddle.net/rainzhao/3L9sfsvf/"&gt;https://jsfiddle.net/rainzhao/3L9sfsvf/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;
&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an empty TS project, add ws to it
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
npx tsc --init
npm install ws @types/ws
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Change rootDir and outDir in tsconfig
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"rootDir": "./src",
"outDir": "./dist",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create a simple websocket server
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });

let senderSocket: null | WebSocket = null;
let receiverSocket: null | WebSocket = null;

wss.on('connection', function connection(ws) {
  ws.on('error', console.error);

  ws.on('message', function message(data: any) {
    const message = JSON.parse(data);

  });

  ws.send('something');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Try running the server
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsc -b
node dist/index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Add message handlers
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { WebSocket, WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });

let senderSocket: null | WebSocket = null;
let receiverSocket: null | WebSocket = null;

wss.on('connection', function connection(ws) {
  ws.on('error', console.error);

  ws.on('message', function message(data: any) {
    const message = JSON.parse(data);
    if (message.type === 'sender') {
      senderSocket = ws;
    } else if (message.type === 'receiver') {
      receiverSocket = ws;
    } else if (message.type === 'createOffer') {
      if (ws !== senderSocket) {
        return;
      }
      receiverSocket?.send(JSON.stringify({ type: 'createOffer', sdp: message.sdp }));
    } else if (message.type === 'createAnswer') {
        if (ws !== receiverSocket) {
          return;
        }
        senderSocket?.send(JSON.stringify({ type: 'createAnswer', sdp: message.sdp }));
    } else if (message.type === 'iceCandidate') {
      if (ws === senderSocket) {
        receiverSocket?.send(JSON.stringify({ type: 'iceCandidate', candidate: message.candidate }));
      } else if (ws === receiverSocket) {
        senderSocket?.send(JSON.stringify({ type: 'iceCandidate', candidate: message.candidate }));
      }
    }
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That is all that you need for a simple one way communication b/w two tabs.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;
&lt;h2&gt;
  
  
  Frontend
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a frontend repo
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Add two routes, one for a sender and one for a receiver
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from 'react'
import './App.css'
import { Route, BrowserRouter, Routes } from 'react-router-dom'
import { Sender } from './components/Sender'
import { Receiver } from './components/Receiver'

function App() {
  return (
    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/sender" element={&amp;lt;Sender /&amp;gt;} /&amp;gt;
        &amp;lt;Route path="/receiver" element={&amp;lt;Receiver /&amp;gt;} /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Remove strict mode in main.tsx to get rid of a bunch of webrtc connections locally (not really needed).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to src folder and create a components folder inside which create two files Sender.tsx and Reciever.tsx&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;5.Create components for sender&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react"

export const Sender = () =&amp;gt; {
    const [socket, setSocket] = useState&amp;lt;WebSocket | null&amp;gt;(null);
    const [pc, setPC] = useState&amp;lt;RTCPeerConnection | null&amp;gt;(null);

    useEffect(() =&amp;gt; {
        const socket = new WebSocket('ws://localhost:8080');
        setSocket(socket);
        socket.onopen = () =&amp;gt; {
            socket.send(JSON.stringify({
                type: 'sender'
            }));
        }
    }, []);

    const initiateConn = async () =&amp;gt; {

        if (!socket) {
            alert("Socket not found");
            return;
        }

        socket.onmessage = async (event) =&amp;gt; {
            const message = JSON.parse(event.data);
            if (message.type === 'createAnswer') {
                await pc.setRemoteDescription(message.sdp);
            } else if (message.type === 'iceCandidate') {
                pc.addIceCandidate(message.candidate);
            }
        }

        const pc = new RTCPeerConnection();
        setPC(pc);
        pc.onicecandidate = (event) =&amp;gt; {
            if (event.candidate) {
                socket?.send(JSON.stringify({
                    type: 'iceCandidate',
                    candidate: event.candidate
                }));
            }
        }

        pc.onnegotiationneeded = async () =&amp;gt; {
            const offer = await pc.createOffer();
            await pc.setLocalDescription(offer);
            socket?.send(JSON.stringify({
                type: 'createOffer',
                sdp: pc.localDescription
            }));
        }

        getCameraStreamAndSend(pc);
    }

    const getCameraStreamAndSend = (pc: RTCPeerConnection) =&amp;gt; {
        navigator.mediaDevices.getUserMedia({ video: true }).then((stream) =&amp;gt; {
            const video = document.createElement('video');
            video.srcObject = stream;
            video.play();
            // this is wrong, should propogate via a component
            document.body.appendChild(video);
            stream.getTracks().forEach((track) =&amp;gt; {
                pc?.addTrack(track);
            });
        });
    }

    return &amp;lt;div&amp;gt;
        Sender
        &amp;lt;button onClick={initiateConn}&amp;gt; Send data &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create the component for receiver
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect } from "react"


export const Receiver = () =&amp;gt; {

    useEffect(() =&amp;gt; {
        const socket = new WebSocket('ws://localhost:8080');
        socket.onopen = () =&amp;gt; {
            socket.send(JSON.stringify({
                type: 'receiver'
            }));
        }
        startReceiving(socket);
    }, []);

    function startReceiving(socket: WebSocket) {
        const video = document.createElement('video');
        document.body.appendChild(video);

        const pc = new RTCPeerConnection();
        pc.ontrack = (event) =&amp;gt; {
            video.srcObject = new MediaStream([event.track]);
            video.play();
        }

        socket.onmessage = (event) =&amp;gt; {
            const message = JSON.parse(event.data);
            if (message.type === 'createOffer') {
                pc.setRemoteDescription(message.sdp).then(() =&amp;gt; {
                    pc.createAnswer().then((answer) =&amp;gt; {
                        pc.setLocalDescription(answer);
                        socket.send(JSON.stringify({
                            type: 'createAnswer',
                            sdp: answer
                        }));
                    });
                });
            } else if (message.type === 'iceCandidate') {
                pc.addIceCandidate(message.candidate);
            }
        }
    }

    return &amp;lt;div&amp;gt;

    &amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And You are good to go!!&lt;/p&gt;

&lt;p&gt;Do like and share it if you found it useful. &lt;br&gt;
Share your views below and comment if you have a doubt. &lt;/p&gt;

&lt;p&gt;Thanks :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Rate Limiting and DDoS</title>
      <dc:creator>Nirvan Jha</dc:creator>
      <pubDate>Sun, 16 Jun 2024 16:46:48 +0000</pubDate>
      <link>https://dev.to/nirvanjha2004/rate-limiting-and-ddos-4j56</link>
      <guid>https://dev.to/nirvanjha2004/rate-limiting-and-ddos-4j56</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Rate Limiting?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rate Limiting is a technique to control the rate at which requests (such as GET, POST, PUT etc.) are made to a service by a client or an application.&lt;/li&gt;
&lt;li&gt;This is achieved by restricting the amount of request that a client makes to the server in a specified amount of time. For example, you can make only 100 requests in a specified time interval say 30 secs. It means if you made 100 requests to a website....then you have to wait for 30 seconds...and then only you are allowed to make 100 request again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why Rate Limiting?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Preventing Overload:- Rate limiting controls how often a user or system can make requests to a service. This helps prevent overuse of resources, ensuring that the system remains available and responsive for all users. For example, rate limiting can stop a single user from making thousands of login attempts in a minute, which could otherwise degrade service for others.&lt;/li&gt;
&lt;li&gt;Mitigating Abuse: Without rate limiting, an application could be more susceptible to abuse such as brute force attacks on passwords. 
(Brute Force Attacks: in these attacks, the attackers tries to gain the access of the system illegally...For ex. If the site needs an 4 digit OTP to Login, So the attacker will run an algorithm and try all the 4 digit numbers from 1000 to 9999. This is a hit and trial method. The algorithm tries each and every OTP ).
By limiting how often someone can perform an action, it reduces the feasibility of such attacks.&lt;/li&gt;
&lt;li&gt;Managing Traffic: In high traffic scenarios such as movie ticket booking...Rate Limiting helps to manage the Traffic on the website and ensures a fairer distribution of services to the users. &lt;/li&gt;
&lt;li&gt;DDoS protection: A DDoS attack means attacking the website from multiple sources which can make the website unavailable. DDoS protection mechanism can identify such malicious traffic and filter them. (Sounds Overwhelming just look at the Figure below).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9206ynbsm3q68vo2ijv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9206ynbsm3q68vo2ijv1.png" alt="Image description" width="800" height="570"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fapp0pdi1ukavicxzyr87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fapp0pdi1ukavicxzyr87.png" alt="Image description" width="800" height="635"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the question arises:-&lt;br&gt;
**Where Can You Commonly Find Rate Limiters=&amp;gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the login page of sites...where there is a higher chance of Brute Force attacks. &lt;/li&gt;
&lt;li&gt;In the Ecommerce Sites: Suppose if there is a sale on Sneakers...The attackers will send multiple request to jam the website so that normal users cant access it.&lt;/li&gt;
&lt;li&gt;API Endpoints , Email Sending etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;**How To Write the Code for Rate Limiter:-&lt;br&gt;
Step1: Add Dependency&lt;br&gt;
npm i express-rate-limit&lt;/p&gt;

&lt;p&gt;Step2: &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vizuxrjy55jcq8p5gwb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vizuxrjy55jcq8p5gwb.png" alt="Image description" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step3: Use this middleware in whichever end point you need. An example is attached Below on how to use this middleware.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm82s82x2yosgpj2wu17d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm82s82x2yosgpj2wu17d.png" alt="Image description" width="495" height="1018"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the PROBLEM arises:- Your Server is still vulnerable to DDoS.&lt;br&gt;
Though DDoS is rarely used for password reset, it is usually used to choke a server.&lt;/p&gt;

&lt;p&gt;How can you save your reset password endpoint?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can implement logic in the rate limiter code that only 3 resets are allowed per email sent out. OR&lt;/li&gt;
&lt;li&gt;You can implement CAPTCHA logic.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How does a captcha work:- You can various tools such as CloudFlare turnstile. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h2u708fpqyd22v0dz11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h2u708fpqyd22v0dz11.png" alt="Image description" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To Use Captcha , Here is an example how you can do it:-&lt;br&gt;
Step1: Go to Cloudflare Turnstile and click on ADD SITE&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj9ysrft8l8w132osi2lc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj9ysrft8l8w132osi2lc.png" alt="Image description" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step2: Add a name and Domain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9tm5pk57a1r97x9bb4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9tm5pk57a1r97x9bb4t.png" alt="Image description" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step3: You will get a site key and a secret key (This secret key is used to verify token...discussed earlier).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6m98csqyusnk8ro2rqdu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6m98csqyusnk8ro2rqdu.png" alt="Image description" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step4:Create a React Page&lt;/p&gt;

&lt;p&gt;Step5: Add Dependencies by running this command in the terminal:-&lt;br&gt;
npm i &lt;a class="mentioned-user" href="https://dev.to/marsidev"&gt;@marsidev&lt;/a&gt;/react-turnstile&lt;/p&gt;

&lt;p&gt;Step6: Update App.tsx&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvht5dk634ls6e0adsen.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvht5dk634ls6e0adsen.png" alt="Image description" width="609" height="802"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step7: Update the backend Code:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozyczr5jshls65aeo5lc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozyczr5jshls65aeo5lc.png" alt="Image description" width="482" height="1022"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And You are Good to Go :)&lt;/p&gt;

&lt;p&gt;Do Share your reviews in the comments!! &lt;br&gt;
Thanks&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
