Advanced Backend
Advanced Backend
For example, for a website like PayTM, whenever you do a transaction, the following might
happen
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 1/12
7/8/24, 11:31 PM DailyCode
For leetcode, whenever the user submits a problem, the following might happen -
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 2/12
7/8/24, 11:31 PM DailyCode
Types of communication
Synchronous (Strong coupling)
1. HTTP (REST/GraphQL)
2. Pub subs
3. Server-Sent Events
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 3/12
7/8/24, 11:31 PM DailyCode
Websockets
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.
Live Feeds: Financial tickers, news feeds, and social media updates are examples where
WebSockets can be used to push live data to users.
Interactive Services: Collaborative editing tools, live customer support chat, and interactive
webinars can use WebSockets to enhance user interactio
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 4/12
7/8/24, 11:31 PM DailyCode
2. No way to push server side events (You can use polling but not the best approach)
Websockets in Node.js
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 5/12
7/8/24, 11:31 PM DailyCode
There are various libraries that let you create a ws server (similar to how express lets you create
an HTTP server)
1. https://www.npmjs.com/package/websocket
2. https://github.com/websockets/ws
3. https://socket.io/
Ws in Node.js (Code)
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 6/12
7/8/24, 11:31 PM DailyCode
Add tsconfig to it
Update tsconfig
Install ws
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 7/12
7/8/24, 11:31 PM DailyCode
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 8/12
7/8/24, 11:31 PM DailyCode
Will work in a raw project , React project and Next project (needs to be client side)
function App() {
const [socket, setSocket] = useState<WebSocket | null>(null);
useEffect(() => {
const newSocket = new WebSocket('ws://localhost:8080');
newSocket.onopen = () => {
console.log('Connection established');
newSocket.send('Hello Server!');
}
newSocket.onmessage = (message) => {
console.log('Message received:', message.data);
}
setSocket(newSocket);
return () => newSocket.close();
}, [])
return (
<>
hi there
</>
)
}
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 9/12
7/8/24, 11:31 PM DailyCode
Next.js
Create a fresh next project
useEffect(() => {
const newSocket = new WebSocket('ws://localhost:8080');
newSocket.onopen = () => {
console.log('Connection established');
newSocket.send('Hello Server!');
}
newSocket.onmessage = (message) => {
console.log('Message received:', message.data);
}
setSocket(newSocket);
return () => newSocket.close();
}, [])
return (
<>
hi there
</>
)
}
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 10/12
7/8/24, 11:31 PM DailyCode
Scaling ws servers
In the real world, you’d want more than one websocket servers (Especially as your website gets
more traffic)
The way to scale websocket servers usually happens by creating a ws fleet
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 11/12
7/8/24, 11:31 PM DailyCode
https://projects.100xdevs.com/pdf/ABEC/ABEC-4 12/12