
Introduction
Imagine you’re at an international conference where people speak different languages. To communicate effectively, you need translators and agreed-upon rules for conversation. Application layer protocols serve the same purpose for computers—they’re the standardized languages that allow different applications to communicate over a network.
In this comprehensive guide, we’ll explore 11 essential application layer protocols that power everything from browsing websites to sending emails and controlling IoT devices.
What is the Application Layer?
The application layer sits at the top of the OSI (Open Systems Interconnection) model and the TCP/IP protocol suite. It’s the layer closest to end-users and provides network services directly to applications. Think of it as the interface between software applications and the network itself.
1. HTTP (Hypertext Transfer Protocol)

What is HTTP?
HTTP is the foundation of data communication on the World Wide Web. It’s a request-response protocol where clients (usually web browsers) request resources from servers, and servers respond with the requested data.
How HTTP Works
When you type a URL into your browser, here’s what happens:
- Your browser creates an HTTP request
- The request travels through the internet to the web server
- The server processes the request
- The server sends back an HTTP response
- Your browser displays the content
HTTP Request Structure
GET /api/users/123 HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Breaking it down:
- Method: GET (retrieve data), POST (submit data), PUT (update), DELETE (remove), PATCH (partial update)
- Path: /api/users/123 (the resource location)
- Headers: Metadata about the request
- Body: Data payload (for POST/PUT requests)
HTTP Response Structure
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 156
Cache-Control: max-age=3600
{
"id": 123,
"name": "Alice Johnson",
"email": "alice@example.com"
}
Real-World Examples
Example 1: Social Media Feed
When you open Instagram:
- Your app sends:
GET /api/feed HTTP/1.1 - Server responds with JSON containing your personalized feed
- Images are loaded via additional GET requests
- Liking a post sends:
POST /api/posts/456/like
Example 2: E-commerce Checkout
// Adding item to cart
fetch("https://shop.example.com/api/cart", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer token123",
},
body: JSON.stringify({
productId: "prod_789",
quantity: 2,
}),
});
Example 3: Weather App
// Fetching weather data
const response = await fetch("https://api.weather.com/v1/forecast?city=Paris");
const weatherData = await response.json();
// Returns: { temperature: 22, condition: "Sunny", humidity: 65 }
HTTP/2 and HTTP/3
HTTP/2 introduced:
- Multiplexing (multiple requests over single connection)
- Server push (servers can send resources before requested)
- Header compression
HTTP/3 uses QUIC protocol over UDP for:
- Faster connection establishment
- Better performance on lossy networks
- Improved mobile experience
HTTPS (HTTP Secure)
HTTPS encrypts data using TLS/SSL. When you see the padlock icon in your browser, you’re using HTTPS. This prevents:
- Eavesdropping on your data
- Man-in-the-middle attacks
- Data tampering
Real example: Banking websites use HTTPS to protect your credentials and financial transactions.
2. WebSocket

What is WebSocket?
WebSocket provides full-duplex communication channels over a single TCP connection. Unlike HTTP’s request-response model, WebSocket allows both client and server to send messages independently at any time.
The Problem WebSocket Solves
Before WebSocket, real-time communication required:
- Polling: Client repeatedly asks “Any updates?” (inefficient)
- Long polling: Server holds request until data available (complex)
- Server-Sent Events: One-way communication only
WebSocket provides true bidirectional communication with minimal overhead.
How WebSocket Works
// Client establishes WebSocket connection
const socket = new WebSocket("wss://chat.example.com");
// Connection opened
socket.addEventListener("open", event => {
console.log("Connected to server");
socket.send("Hello Server!");
});
// Listen for messages
socket.addEventListener("message", event => {
console.log("Message from server:", event.data);
});
// Send messages
function sendMessage(text) {
socket.send(
JSON.stringify({
type: "chat",
content: text,
timestamp: Date.now(),
})
);
}
Real-World Examples
Example 1: Chat Application (Slack, Discord)
// User sends message
socket.send(
JSON.stringify({
channel: "general",
message: "Hello team!",
user: "alice",
})
);
// All connected users receive instantly
socket.onmessage = event => {
const data = JSON.parse(event.data);
displayMessage(data.user, data.message);
};
Example 2: Live Sports Scores
// Sports app maintains WebSocket connection
const sportsSocket = new WebSocket("wss://scores.espn.com/live");
sportsSocket.onmessage = event => {
const update = JSON.parse(event.data);
// { game: "Lakers vs Warriors", score: "102-98", quarter: 4 }
updateScoreboard(update);
};
Example 3: Collaborative Editing (Google Docs)
// User types in document
document.addEventListener("input", e => {
socket.send(
JSON.stringify({
type: "edit",
position: cursorPosition,
text: e.target.value,
documentId: "doc_123",
})
);
});
// Receive edits from other users
socket.onmessage = event => {
const edit = JSON.parse(event.data);
applyRemoteEdit(edit);
};
Example 4: Stock Trading Platform
Real-time stock prices update without page refresh:
const stockSocket = new WebSocket("wss://trading.example.com");
stockSocket.onmessage = event => {
const priceUpdate = JSON.parse(event.data);
// { symbol: "AAPL", price: 178.32, change: +2.15 }
updateStockPrice(priceUpdate);
};
Example 5: Multiplayer Online Games
// Player moves
socket.send(
JSON.stringify({
action: "move",
playerId: "player_456",
position: { x: 100, y: 250 },
direction: "north",
})
);
// Receive all players' positions
socket.onmessage = event => {
const gameState = JSON.parse(event.data);
renderPlayers(gameState.players);
};
3. WebRTC (Web Real-Time Communication)

What is WebRTC?
WebRTC enables peer-to-peer audio, video, and data sharing directly between browsers without requiring plugins or intermediate servers (after initial connection setup).
Key Components
- MediaStream: Access camera/microphone
- RTCPeerConnection: Audio/video communication
- RTCDataChannel: Arbitrary data transfer
How WebRTC Works
// Get user's camera and microphone
const localStream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: true,
});
// Display local video
const localVideo = document.getElementById("localVideo");
localVideo.srcObject = localStream;
// Create peer connection
const peerConnection = new RTCPeerConnection({
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
});
// Add local stream to connection
localStream.getTracks().forEach(track => {
peerConnection.addTrack(track, localStream);
});
// Handle incoming remote stream
peerConnection.ontrack = event => {
const remoteVideo = document.getElementById("remoteVideo");
remoteVideo.srcObject = event.streams[0];
};
Real-World Examples
Example 1: Video Conferencing (Zoom, Google Meet)
// Starting a video call
async function startCall() {
// Get local media
const stream = await navigator.mediaDevices.getUserMedia({
video: { width: 1280, height: 720 },
audio: { echoCancellation: true, noiseSuppression: true },
});
// Create connection for each participant
participants.forEach(participant => {
const pc = new RTCPeerConnection(config);
stream.getTracks().forEach(track => pc.addTrack(track, stream));
connections.set(participant.id, pc);
});
}
Example 2: Screen Sharing
// Share screen in meeting
async function shareScreen() {
const screenStream = await navigator.mediaDevices.getDisplayMedia({
video: { cursor: "always" },
audio: false,
});
// Replace video track with screen track
const screenTrack = screenStream.getVideoTracks()[0];
const sender = peerConnection
.getSenders()
.find(s => s.track.kind === "video");
sender.replaceTrack(screenTrack);
}
Example 3: Peer-to-Peer File Sharing
// Send file directly to peer
const dataChannel = peerConnection.createDataChannel("fileTransfer");
function sendFile(file) {
const chunkSize = 16384;
const reader = new FileReader();
reader.onload = e => {
dataChannel.send(e.target.result);
};
// Read and send file in chunks
for (let offset = 0; offset < file.size; offset += chunkSize) {
const slice = file.slice(offset, offset + chunkSize);
reader.readAsArrayBuffer(slice);
}
}
Example 4: Live Streaming (Twitch-style)
WebRTC enables low-latency broadcasting:
// Broadcaster side
const broadcast = await navigator.mediaDevices.getUserMedia({
video: true,
audio: true,
});
viewers.forEach(viewer => {
const pc = new RTCPeerConnection();
broadcast.getTracks().forEach(track => pc.addTrack(track, broadcast));
// Send stream to viewer
});
Example 5: IoT Device Communication
// Control robot camera via WebRTC
const robotConnection = new RTCPeerConnection();
// Receive robot camera feed
robotConnection.ontrack = event => {
document.getElementById("robotView").srcObject = event.streams[0];
};
// Send control commands via data channel
const controlChannel = robotConnection.createDataChannel("controls");
controlChannel.send(JSON.stringify({ action: "moveForward", speed: 50 }));
4. MQTT (Message Queuing Telemetry Transport)

What is MQTT?
MQTT is a lightweight publish-subscribe messaging protocol designed for constrained devices and low-bandwidth, high-latency networks. It’s perfect for IoT (Internet of Things) applications.
The Publish-Subscribe Pattern
Instead of direct communication, MQTT uses a broker:
- Publishers send messages to topics
- Subscribers receive messages from topics they’re interested in
- Broker routes messages between publishers and subscribers
MQTT Structure
// Connect to MQTT broker
const mqtt = require("mqtt");
const client = mqtt.connect("mqtt://broker.example.com:1883", {
clientId: "sensor_kitchen_temp",
username: "device_user",
password: "secure_password",
});
// Publish temperature reading
client.on("connect", () => {
setInterval(() => {
const temperature = readTemperatureSensor();
client.publish("home/kitchen/temperature", temperature.toString(), {
qos: 1, // Quality of Service level
retain: true, // Broker retains last message for new subscribers
});
}, 5000);
});
// Subscribe to thermostat commands
client.subscribe("home/kitchen/thermostat/set", { qos: 1 });
client.on("message", (topic, message) => {
if (topic === "home/kitchen/thermostat/set") {
const targetTemp = parseInt(message.toString());
adjustThermostat(targetTemp);
}
});
Quality of Service Levels
- QoS 0: At most once (fire and forget)
- QoS 1: At least once (acknowledged delivery)
- QoS 2: Exactly once (guaranteed delivery)
Real-World Examples
Example 1: Smart Home System
// Living room motion sensor
client.publish("home/livingroom/motion", "detected", { qos: 1 });
// Smart lights subscribe and turn on
client.subscribe("home/livingroom/motion");
client.on("message", (topic, message) => {
if (message.toString() === "detected") {
client.publish("home/livingroom/lights", "on");
}
});
Example 2: Agricultural Monitoring
// Soil moisture sensors across farm
const sensors = ["field1", "field2", "field3", "greenhouse"];
sensors.forEach(location => {
setInterval(() => {
const moisture = readMoistureSensor(location);
client.publish(`farm/${location}/soil/moisture`, moisture.toString());
// Alert if too dry
if (moisture < 30) {
client.publish(`farm/${location}/irrigation/needed`, "true", {
qos: 2, // Critical message
retain: true,
});
}
}, 60000); // Every minute
});
Example 3: Fleet Vehicle Tracking
// Each vehicle publishes GPS location
function publishVehicleLocation(vehicleId, lat, lon) {
client.publish(
`fleet/${vehicleId}/gps`,
JSON.stringify({
latitude: lat,
longitude: lon,
timestamp: Date.now(),
speed: getCurrentSpeed(),
}),
{ qos: 1 }
);
}
// Control center subscribes to all vehicles
client.subscribe("fleet/+/gps"); // '+' is single-level wildcard
client.on("message", (topic, message) => {
const vehicleId = topic.split("/")[1];
const location = JSON.parse(message.toString());
updateMapDisplay(vehicleId, location);
});
Example 4: Industrial Factory Automation
// Production line machines report status
client.publish(
"factory/line1/machine5/status",
JSON.stringify({
operational: true,
temperature: 85,
unitsProduced: 1523,
lastMaintenance: "2025-10-01",
})
);
// Subscribe to all machine statuses
client.subscribe("factory/+/+/status"); // Multi-level wildcard
// Emergency stop broadcast
client.subscribe("factory/emergency/stop");
client.on("message", (topic, message) => {
if (topic === "factory/emergency/stop") {
haltAllMachines();
alertSafetySystems();
}
});
Example 5: Healthcare Patient Monitoring
// Wearable device publishes vital signs
const patientMonitor = {
publish: () => {
client.publish(
`hospital/room205/patient/${patientId}/heartrate`,
heartRate.toString(),
{ qos: 2 }
);
client.publish(
`hospital/room205/patient/${patientId}/oxygen`,
oxygenLevel.toString(),
{ qos: 2 }
);
},
};
// Nurse station subscribes
client.subscribe("hospital/room205/patient/+/heartrate");
client.subscribe("hospital/room205/patient/+/oxygen");
// Alert on critical values
client.on("message", (topic, message) => {
const value = parseInt(message.toString());
if (topic.includes("heartrate") && (value < 60 || value > 100)) {
triggerAlarm("Abnormal heart rate detected");
}
});
5. AMQP (Advanced Message Queuing Protocol)

What is AMQP?
AMQP is an open standard application layer protocol for message-oriented middleware. It’s more feature-rich than MQTT and designed for enterprise-level messaging with guaranteed delivery, transactions, and complex routing.
Key Concepts
- Producer: Sends messages
- Exchange: Routes messages based on rules
- Queue: Stores messages
- Consumer: Receives messages
- Binding: Rules connecting exchanges to queues
Exchange Types
- Direct: Routes based on exact routing key match
- Topic: Routes based on pattern matching
- Fanout: Broadcasts to all bound queues
- Headers: Routes based on message headers
How AMQP Works
const amqp = require("amqplib");
// Producer
async function sendOrder(order) {
const connection = await amqp.connect("amqp://localhost");
const channel = await connection.createChannel();
await channel.assertExchange("orders", "topic", { durable: true });
const routingKey = `order.${order.type}.${order.priority}`;
channel.publish("orders", routingKey, Buffer.from(JSON.stringify(order)), {
persistent: true, // Survive broker restart
priority: order.priority,
});
console.log(`Sent order: ${routingKey}`);
}
// Consumer
async function processOrders() {
const connection = await amqp.connect("amqp://localhost");
const channel = await connection.createChannel();
await channel.assertExchange("orders", "topic", { durable: true });
await channel.assertQueue("high_priority_orders", { durable: true });
// Bind queue to exchange with pattern
await channel.bindQueue("high_priority_orders", "orders", "order.*.high");
channel.consume("high_priority_orders", msg => {
const order = JSON.parse(msg.content.toString());
console.log("Processing high priority order:", order);
// Acknowledge message processing
channel.ack(msg);
});
}
Real-World Examples
Example 1: E-commerce Order Processing
// Order service publishes new orders
async function placeOrder(orderData) {
const channel = await getChannel();
// Publish to fanout exchange - all services get notified
channel.publish(
"new_orders",
"",
Buffer.from(
JSON.stringify({
orderId: orderData.id,
customerId: orderData.customerId,
items: orderData.items,
total: orderData.total,
})
),
{ persistent: true }
);
}
// Inventory service consumes
const inventoryQueue = await channel.assertQueue("inventory_updates");
await channel.bindQueue("inventory_updates", "new_orders", "");
channel.consume("inventory_updates", async msg => {
const order = JSON.parse(msg.content.toString());
await reduceInventory(order.items);
channel.ack(msg);
});
// Payment service consumes
const paymentQueue = await channel.assertQueue("payment_processing");
await channel.bindQueue("payment_processing", "new_orders", "");
channel.consume("payment_processing", async msg => {
const order = JSON.parse(msg.content.toString());
await processPayment(order.customerId, order.total);
channel.ack(msg);
});
// Shipping service consumes
const shippingQueue = await channel.assertQueue("shipping_labels");
await channel.bindQueue("shipping_labels", "new_orders", "");
Example 2: Video Processing Pipeline
// Upload service publishes video processing tasks
async function queueVideoProcessing(video) {
const channel = await getChannel();
await channel.assertExchange("video_processing", "direct");
const tasks = [
{ type: "transcode", format: "1080p", queue: "high_quality" },
{ type: "transcode", format: "720p", queue: "medium_quality" },
{ type: "transcode", format: "480p", queue: "low_quality" },
{ type: "thumbnail", queue: "thumbnail_gen" },
];
tasks.forEach(task => {
channel.publish(
"video_processing",
task.queue,
Buffer.from(
JSON.stringify({
videoId: video.id,
sourceUrl: video.url,
...task,
})
)
);
});
}
// Worker nodes consume based on capability
channel.consume(
"high_quality",
async msg => {
const job = JSON.parse(msg.content.toString());
await transcodeVideo(job.videoId, job.sourceUrl, "1080p");
channel.ack(msg);
},
{ prefetch: 1 }
); // Process one at a time
Example 3: Distributed Logging System
// Applications publish logs to topic exchange
function logMessage(level, service, message) {
const routingKey = `log.${level}.${service}`;
channel.publish(
"logs",
routingKey,
Buffer.from(
JSON.stringify({
timestamp: new Date(),
level,
service,
message,
})
)
);
}
// Error monitoring service subscribes to errors
await channel.bindQueue("error_alerts", "logs", "log.error.*");
await channel.bindQueue("error_alerts", "logs", "log.critical.*");
// General logging service gets everything
await channel.bindQueue("all_logs", "logs", "log.#"); // # matches all
// Analytics service only wants specific services
await channel.bindQueue("analytics_logs", "logs", "log.*.analytics");
await channel.bindQueue("analytics_logs", "logs", "log.*.api");
Example 4: Microservices Communication
// User service notifies other services of user events
async function publishUserEvent(event, userData) {
await channel.publish(
"user_events",
`user.${event}`,
Buffer.from(JSON.stringify(userData))
);
}
// When user registers
publishUserEvent("registered", { userId: 123, email: "user@example.com" });
// Email service listens
await channel.bindQueue("email_queue", "user_events", "user.registered");
channel.consume("email_queue", async msg => {
const user = JSON.parse(msg.content.toString());
await sendWelcomeEmail(user.email);
channel.ack(msg);
});
// Analytics service listens
await channel.bindQueue("analytics_queue", "user_events", "user.*");
// Notification service listens
await channel.bindQueue("notifications_queue", "user_events", "user.#");
6. SMTP (Simple Mail Transfer Protocol)

What is SMTP?
SMTP is the standard protocol for sending emails across the internet. It handles the transmission of email from your mail client to mail servers and between mail servers.
How SMTP Works
The email journey:
- You compose email in client (Gmail, Outlook)
- Client sends to SMTP server using SMTP
- SMTP server looks up recipient’s mail server (DNS MX record)
- SMTP server connects to recipient’s server
- Email delivered to recipient’s mailbox
- Recipient retrieves with IMAP/POP3
SMTP Commands
HELO/EHLO: Identify client to server
MAIL FROM: Specify sender
RCPT TO: Specify recipient
DATA: Begin message content
QUIT: Close connection
SMTP Example Session
Client: EHLO client.example.com
Server: 250-smtp.gmail.com
Server: 250-STARTTLS
Server: 250 AUTH LOGIN
Client: MAIL FROM:<sender@example.com>
Server: 250 OK
Client: RCPT TO:<recipient@example.com>
Server: 250 OK
Client: DATA
Server: 354 Start mail input
Client: Subject: Meeting Tomorrow
Client: From: sender@example.com
Client: To: recipient@example.com
Client:
Client: Hi, just confirming our 2pm meeting tomorrow.
Client: .
Server: 250 OK Message accepted
Client: QUIT
Server: 221 Bye
Real-World Examples
Example 1: Sending Email with Node.js
const nodemailer = require("nodemailer");
// Create SMTP transporter
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "your-email@gmail.com",
pass: "your-app-password",
},
});
// Send welcome email
async function sendWelcomeEmail(userEmail, userName) {
const info = await transporter.sendMail({
from: '"MyApp Team" <noreply@myapp.com>',
to: userEmail,
subject: "Welcome to MyApp!",
html: `
<h1>Welcome, ${userName}!</h1>
<p>Thanks for signing up. Get started by completing your profile.</p>
<a href="https://myapp.com/setup">Complete Setup</a>
`,
});
console.log("Message sent: %s", info.messageId);
}
Example 2: Order Confirmation Email
async function sendOrderConfirmation(order) {
await transporter.sendMail({
from: '"ShopName" <orders@shopname.com>',
to: order.customerEmail,
subject: `Order Confirmation #${order.id}`,
html: `
<h2>Thanks for your order!</h2>
<p>Order #${order.id}</p>
<ul>
${order.items
.map(
item => `
<li>${item.name} - $${item.price} x ${item.quantity}</li>
`
)
.join("")}
</ul>
<p><strong>Total: $${order.total}</strong></p>
<p>Estimated delivery: ${order.deliveryDate}</p>
`,
attachments: [
{
filename: `invoice-${order.id}.pdf`,
path: `/invoices/${order.id}.pdf`,
},
],
});
}
Example 3: Password Reset Email
async function sendPasswordReset(email, resetToken) {
const resetUrl = `https://myapp.com/reset-password?token=${resetToken}`;
await transporter.sendMail({
from: '"Security" <security@myapp.com>',
to: email,
subject: "Password Reset Request",
html: `
<h2>Password Reset Request</h2>
<p>Click the link below to reset your password:</p>
<a href="${resetUrl}">Reset Password</a>
<p>This link expires in 1 hour.</p>
<p>If you didn't request this, please ignore this email.</p>
`,
});
}
Example 4: Bulk Newsletter Campaign
async function sendNewsletter(subscribers, content) {
const batchSize = 100;
for (let i = 0; i < subscribers.length; i += batchSize) {
const batch = subscribers.slice(i, i + batchSize);
const promises = batch.map(subscriber => {
return transporter.sendMail({
from: '"Newsletter" <news@company.com>',
to: subscriber.email,
subject: "Monthly Update - October 2025",
html: `
<h1>Hi ${subscriber.name},</h1>
${content.html}
<a href="https://company.com/unsubscribe/${subscriber.id}">
Unsubscribe
</a>
`,
});
});
await Promise.all(promises);
// Rate limiting: wait 1 second between batches
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
Example 5: Alert Notifications
// Critical system alert
async function sendCriticalAlert(issue) {
await transporter.sendMail({
from: '"System Monitor" <alerts@company.com>',
to: "admin@company.com, ops@company.com",
priority: "high",
subject: `🚨 CRITICAL: ${issue.title}`,
html: `
<h1 style="color: red;">Critical System Alert</h1>
<p><strong>Service:</strong> ${issue.service}</p>
<p><strong>Error:</strong> ${issue.message}</p>
<p><strong>Time:</strong> ${new Date().toISOString()}</p>
<p><strong>Impact:</strong> ${issue.affectedUsers} users affected</p>
<a href="https://dashboard.company.com/incidents/${issue.id}">
View Details
</a>
`,
});
}
7. IMAP (Internet Message Access Protocol)

What is IMAP?
IMAP allows you to access and manage your email on a mail server. Unlike POP3, IMAP synchronizes email across multiple devices and keeps messages on the server.
Key IMAP Features
- Multi-device sync: Changes reflect everywhere
- Server-side folders: Organize on server
- Partial fetch: Download headers first, bodies on demand
- Search on server: Fast searching without downloading everything
- Multiple mailbox support: Access different accounts
How IMAP Works
Client: A001 LOGIN username password
Server: A001 OK LOGIN completed
Client: A002 SELECT INBOX
Server: * 172 EXISTS (172 messages in inbox)
Server: * 5 RECENT (5 new messages)
Server: A002 OK SELECT completed
Client: A003 FETCH 1:10 (FLAGS BODY[HEADER.FIELDS (FROM SUBJECT DATE)])
Server: * 1 FETCH (FLAGS (\Seen) BODY[HEADER.FIELDS...])
Server: A003 OK FETCH completed
Client: A004 SEARCH FROM "boss@company.com" SINCE 01-Oct-2025
Server: * SEARCH 15 27 38
Server: A004 OK SEARCH completed
Real-World Examples
Example 1: Email Client Application
const Imap = require("imap");
const imap = new Imap({
user: "user@gmail.com",
password: "app-password",
host: "imap.gmail.com",
port: 993,
tls: true,
});
function openInbox(callback) {
imap.openBox("INBOX", false, callback);
}
imap.once("ready", () => {
openInbox((err, box) => {
if (err) throw err;
// Fetch unseen messages from last 7 days
const searchCriteria = [
"UNSEEN",
["SINCE", new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)],
];
imap.search(searchCriteria, (err, results) => {
if (err || !results.length) {
console.log("No new messages");
return;
}
const fetch = imap.fetch(results, {
bodies: ["HEADER.FIELDS (FROM TO SUBJECT DATE)", "TEXT"],
markSeen: false,
});
fetch.on("message", (msg, seqno) => {
console.log(`Message #${seqno}`);
msg.on("body", (stream, info) => {
let buffer = "";
stream.on("data", chunk => (buffer += chunk.toString("utf8")));
stream.once("end", () => {
console.log("Email content:", buffer);
});
});
});
fetch.once("end", () => {
console.log("Done fetching messages");
imap.end();
});
});
});
});
imap.connect();
Example 2: Smart Email Organizer
// Automatically organize emails into folders
async function organizeEmails() {
imap.once("ready", () => {
imap.openBox("INBOX", false, async (err, box) => {
// Search for receipts
imap.search(["SUBJECT", "receipt", "order"], (err, receipts) => {
if (receipts.length > 0) {
imap.move(receipts, "Receipts", err => {
console.log("Moved receipts to folder");
});
}
});
// Search for newsletters
imap.search(["FROM", "newsletter@"], (err, newsletters) => {
if (newsletters.length > 0) {
imap.move(newsletters, "Newsletters", err => {
console.log("Moved newsletters to folder");
});
}
});
// Mark promotional emails
imap.search(["SUBJECT", "sale", "discount", "offer"], (err, promos) => {
if (promos.length > 0) {
imap.addFlags(promos, ["\\Flagged"], err => {
console.log("Flagged promotional emails");
});
}
});
});
});
}
Example 3: Email Backup System
// Backup all emails to local storage
async function backupEmails() {
const fs = require("fs");
imap.once("ready", () => {
imap.getBoxes((err, boxes) => {
// Iterate through all mailboxes
Object.keys(boxes).forEach(boxName => {
imap.openBox(boxName, true, (err, box) => {
// Search all messages
imap.search(["ALL"], (err, results) => {
if (!results.length) return;
const fetch = imap.fetch(results, { bodies: "" });
fetch.on("message", (msg, seqno) => {
msg.on("body", stream => {
const writeStream = fs.createWriteStream(
`./backup/${boxName}/${seqno}.eml`
);
stream.pipe(writeStream);
});
});
});
});
});
});
});
}
Example 4: Email Monitoring Dashboard
// Monitor inbox for specific keywords
function monitorInbox(keywords, callback) {
imap.once("ready", () => {
imap.openBox("INBOX", false, (err, box) => {
// Initial check
keywords.forEach(keyword => {
imap.search(["SUBJECT", keyword], (err, results) => {
results.forEach(msgId => {
fetchAndProcess(msgId, callback);
});
});
});
// Listen for new messages
imap.on("mail", numNew => {
console.log(`${numNew} new messages arrived`);
keywords.forEach(keyword => {
imap.search(["UNSEEN", ["SUBJECT", keyword]], (err, results) => {
results.forEach(msgId => {
fetchAndProcess(msgId, callback);
});
});
});
});
});
});
}
// Usage: Alert on urgent emails
monitorInbox(["URGENT", "CRITICAL", "EMERGENCY"], email => {
sendSlackNotification(`Urgent email from ${email.from}: ${email.subject}`);
});
8. POP3 (Post Office Protocol version 3)

What is POP3?
POP3 is a simpler email retrieval protocol that downloads emails from server to client, typically deleting them from the server afterward. It’s designed for single-device access.
POP3 vs IMAP
| Feature | POP3 | IMAP |
|---|---|---|
| Email location | Downloaded to device | Stays on server |
| Multi-device sync | No | Yes |
| Storage | Uses device storage | Uses server storage |
| Offline access | Full | Limited to cached |
| Speed | Faster download | Faster browsing |
How POP3 Works
Client: USER username
Server: +OK User accepted
Client: PASS password
Server: +OK Password accepted
Client: STAT
Server: +OK 5 3250 (5 messages, 3250 bytes total)
Client: LIST
Server: +OK 5 messages
Server: 1 500
Server: 2 750
Server: 3 600
Server: 4 800
Server: 5 600
Server: .
Client: RETR 1
Server: +OK 500 bytes
Server: (email content)
Server: .
Client: DELE 1
Server: +OK Message deleted
Client: QUIT
Server: +OK Goodbye
Real-World Examples
Example 1: Simple Email Download
const POP3Client = require("poplib");
const client = new POP3Client(995, "pop.gmail.com", {
tlserrs: false,
enabletls: true,
});
client.on("connect", () => {
client.login("user@gmail.com", "app-password");
});
client.on("login", (status, data) => {
if (status) {
client.list(); // Get list of messages
}
});
client.on("list", (status, msgcount, msgnumber, data) => {
if (status) {
// Retrieve all messages
for (let i = 1; i <= msgcount; i++) {
client.retr(i);
}
}
});
client.on("retr", (status, msgnumber, data) => {
console.log(`Message ${msgnumber}:`, data);
// Delete from server after downloading
client.dele(msgnumber);
});
client.on("quit", (status, data) => {
console.log("Disconnected");
});
Example 2: Email Archive Tool
// Download and archive all emails locally
async function archiveAllEmails() {
const client = new POP3Client(995, "pop.gmail.com", {
enabletls: true,
});
client.on("login", status => {
if (status) {
client.list();
}
});
const emails = [];
client.on("list", (status, msgcount) => {
console.log(`Downloading ${msgcount} emails...`);
for (let i = 1; i <= msgcount; i++) {
client.retr(i);
}
});
client.on("retr", (status, msgnumber, data) => {
emails.push({
id: msgnumber,
content: data,
downloadedAt: new Date(),
});
// Save to database or file
saveToDatabase(emails[emails.length - 1]);
// Keep on server (don't delete)
// client.dele(msgnumber); // Uncomment to delete
});
client.connect();
}
Example 3: Vacation Email Responder
// Check emails and send auto-reply
const autoResponder = {
sentTo: new Set(), // Track who we've replied to
checkAndRespond: function () {
client.on("retr", (status, msgnumber, emailData) => {
const email = parseEmail(emailData);
const sender = email.from;
// Send auto-reply if haven't already
if (!this.sentTo.has(sender)) {
sendAutoReply(sender);
this.sentTo.add(sender);
}
// Don't delete - keep on server
});
},
};
function sendAutoReply(recipientEmail) {
transporter.sendMail({
from: "me@example.com",
to: recipientEmail,
subject: "Auto-Reply: Out of Office",
text: "Thank you for your email. I am currently out of office and will respond when I return on October 15th.",
});
}
9. FTP (File Transfer Protocol)

What is FTP?
FTP is a standard protocol for transferring files between a client and server over a network. It uses two connections: one for commands (port 21) and one for data transfer (port 20).
FTP Modes
Active Mode: Server initiates data connection to client Passive Mode: Client initiates both connections (better for firewalls)
FTP Commands
USER: Username for authentication
PASS: Password for authentication
LIST: List files in directory
CWD: Change working directory
RETR: Download file
STOR: Upload file
DELE: Delete file
MKD: Make directory
RMD: Remove directory
PWD: Print working directory
QUIT: Close connection
Real-World Examples
Example 1: Website Deployment
const ftp = require("basic-ftp");
async function deployWebsite() {
const client = new ftp.Client();
client.ftp.verbose = true;
try {
await client.access({
host: "ftp.mywebsite.com",
user: "webmaster",
password: "secure_password",
secure: true, // FTPS
});
console.log("Connected to FTP server");
// Navigate to web root
await client.cd("/public_html");
// Upload entire build folder
await client.uploadFromDir("./build");
console.log("Website deployed successfully");
} catch (err) {
console.error("Deployment failed:", err);
}
client.close();
}
Example 2: Automated Backup
async function backupDatabase() {
const client = new ftp.Client();
try {
await client.access({
host: "backup.company.com",
user: "backup_user",
password: process.env.FTP_PASSWORD,
});
// Create dated backup folder
const date = new Date().toISOString().split("T")[0];
await client.ensureDir(`/backups/${date}`);
await client.cd(`/backups/${date}`);
// Upload database dump
await client.uploadFrom("./db_backup.sql", "database.sql");
// Upload file archives
await client.uploadFrom("./files_backup.tar.gz", "files.tar.gz");
console.log("Backup completed");
// Clean up old backups (keep last 30 days)
await cleanOldBackups(client);
} catch (err) {
console.error("Backup failed:", err);
}
client.close();
}
async function cleanOldBackups(client) {
await client.cd("/backups");
const list = await client.list();
const thirtyDaysAgo = Date.now() - 30 * 24 * 60 * 60 * 1000;
for (const item of list) {
const itemDate = new Date(item.name);
if (itemDate < thirtyDaysAgo) {
await client.removeDir(item.name);
console.log(`Deleted old backup: ${item.name}`);
}
}
}
Example 3: File Synchronization
async function syncFiles(localDir, remoteDir) {
const client = new ftp.Client();
try {
await client.access({
host: "ftp.fileserver.com",
user: "sync_user",
password: "password",
});
await client.cd(remoteDir);
// Get list of remote files
const remoteFiles = await client.list();
const remoteFileNames = remoteFiles.map(f => f.name);
// Get local files
const fs = require("fs").promises;
const localFiles = await fs.readdir(localDir);
// Upload new/modified local files
for (const localFile of localFiles) {
const localPath = `${localDir}/${localFile}`;
const stat = await fs.stat(localPath);
if (stat.isFile()) {
const remoteFile = remoteFiles.find(f => f.name === localFile);
// Upload if new or modified
if (!remoteFile || stat.mtime > new Date(remoteFile.modifiedAt)) {
await client.uploadFrom(localPath, localFile);
console.log(`Uploaded: ${localFile}`);
}
}
}
// Download remote files not present locally
for (const remoteFile of remoteFiles) {
if (!localFiles.includes(remoteFile.name)) {
await client.downloadTo(
`${localDir}/${remoteFile.name}`,
remoteFile.name
);
console.log(`Downloaded: ${remoteFile.name}`);
}
}
} catch (err) {
console.error("Sync failed:", err);
}
client.close();
}
Example 4: Media File Distribution
// Upload videos to CDN via FTP
async function uploadMediaToCDN(videoFiles) {
const client = new ftp.Client();
try {
await client.access({
host: "cdn.mediaserver.com",
user: "media_uploader",
password: process.env.CDN_PASSWORD,
});
await client.cd("/videos");
for (const video of videoFiles) {
// Create folder structure: /videos/2025/10/
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
await client.ensureDir(`${year}/${month}`);
await client.cd(`${year}/${month}`);
// Upload with progress tracking
console.log(`Uploading ${video.name}...`);
await client.uploadFrom(video.path, video.name);
// Generate thumbnail
await client.uploadFrom(video.thumbnailPath, `${video.name}.thumb.jpg`);
console.log(`✓ ${video.name} uploaded`);
await client.cd("/videos");
}
} catch (err) {
console.error("Upload failed:", err);
}
client.close();
}
Example 5: Log File Collection
// Collect log files from multiple servers
async function collectLogs() {
const servers = [
{ host: "web1.company.com", user: "logger", pass: "pass1" },
{ host: "web2.company.com", user: "logger", pass: "pass2" },
{ host: "api.company.com", user: "logger", pass: "pass3" },
];
const today = new Date().toISOString().split("T")[0];
for (const server of servers) {
const client = new ftp.Client();
try {
await client.access(server);
await client.cd("/var/log/application");
// Download today's log file
const logFileName = `app-${today}.log`;
await client.downloadTo(
`./logs/${server.host}/${logFileName}`,
logFileName
);
console.log(`Downloaded logs from ${server.host}`);
} catch (err) {
console.error(`Failed to get logs from ${server.host}:`, err);
}
client.close();
}
// Aggregate and analyze logs
analyzeLogs("./logs");
}
10. SSH (Secure Shell)

What is SSH?
SSH is a cryptographic network protocol for secure remote login and command execution. It provides strong authentication and encrypted communication over insecure networks.
SSH Key Components
SSH Protocol: Secure communication layer Authentication: Password, public key, or multi-factor Encryption: AES, ChaCha20 for data confidentiality Port Forwarding: Tunnel other protocols through SSH
SSH Connection Process
# Basic SSH connection
ssh user@hostname
# With specific port
ssh -p 2222 user@hostname
# Using private key
ssh -i ~/.ssh/id_rsa user@hostname
# Execute remote command
ssh user@hostname 'ls -la /var/www'
# Port forwarding (tunnel)
ssh -L 8080:localhost:80 user@hostname
Real-World Examples
Example 1: Remote Server Management
const { Client } = require("ssh2");
async function executeRemoteCommand(command) {
const conn = new Client();
return new Promise((resolve, reject) => {
conn
.on("ready", () => {
console.log("SSH connection established");
conn.exec(command, (err, stream) => {
if (err) return reject(err);
let output = "";
stream.on("data", data => {
output += data.toString();
console.log("STDOUT:", data.toString());
});
stream.stderr.on("data", data => {
console.log("STDERR:", data.toString());
});
stream.on("close", code => {
console.log("Command exited with code:", code);
conn.end();
resolve(output);
});
});
})
.connect({
host: "server.example.com",
port: 22,
username: "admin",
privateKey: require("fs").readFileSync("/path/to/private/key"),
});
});
}
// Usage
executeRemoteCommand("df -h"); // Check disk space
executeRemoteCommand("systemctl status nginx"); // Check service
executeRemoteCommand("tail -n 100 /var/log/application.log"); // View logs
Example 2: Automated Deployment
async function deployApplication() {
const conn = new Client();
conn
.on("ready", () => {
console.log("Connected to production server");
const commands = [
"cd /var/www/myapp",
"git pull origin main",
"npm install --production",
"npm run build",
"pm2 restart myapp",
"pm2 save",
];
executeCommandSequence(conn, commands, 0);
})
.connect({
host: "prod.myapp.com",
username: "deployer",
privateKey: require("fs").readFileSync("./deploy_key"),
});
}
function executeCommandSequence(conn, commands, index) {
if (index >= commands.length) {
console.log("Deployment complete!");
conn.end();
return;
}
const command = commands[index];
console.log(`Executing: ${command}`);
conn.exec(command, (err, stream) => {
stream.on("close", () => {
executeCommandSequence(conn, commands, index + 1);
});
stream.on("data", data => console.log(data.toString()));
stream.stderr.on("data", data => console.error(data.toString()));
});
}
Example 3: File Transfer with SFTP
async function uploadFileSFTP(localPath, remotePath) {
const conn = new Client();
return new Promise((resolve, reject) => {
conn
.on("ready", () => {
conn.sftp((err, sftp) => {
if (err) return reject(err);
sftp.fastPut(localPath, remotePath, err => {
if (err) return reject(err);
console.log(`Uploaded ${localPath} to ${remotePath}`);
conn.end();
resolve();
});
});
})
.connect({
host: "sftp.server.com",
username: "ftpuser",
password: "secure_password",
});
});
}
// Upload configuration files
uploadFileSFTP("./config.json", "/etc/myapp/config.json");
// Upload SSL certificates
uploadFileSFTP("./ssl/certificate.pem", "/etc/ssl/certs/myapp.pem");
Example 4: Database Tunnel
// Create SSH tunnel to access remote database
async function createDatabaseTunnel() {
const conn = new Client();
conn
.on("ready", () => {
console.log("SSH tunnel established");
conn.forwardOut(
"127.0.0.1", // source address
3306, // source port
"localhost", // destination address
3306, // destination port (on remote server)
(err, stream) => {
if (err) throw err;
// Now connect to database through tunnel
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "127.0.0.1",
port: 3306,
user: "dbuser",
password: "dbpass",
database: "production",
});
connection.query("SELECT * FROM users LIMIT 10", (err, results) => {
console.log(results);
connection.end();
conn.end();
});
}
);
})
.connect({
host: "db.server.com",
username: "sshuser",
privateKey: require("fs").readFileSync("./ssh_key"),
});
}
Example 5: Server Monitoring
// Monitor multiple servers
async function monitorServers(servers) {
const results = {};
for (const server of servers) {
const conn = new Client();
await new Promise(resolve => {
conn
.on("ready", () => {
const checks = [];
// Check CPU usage
checks.push(execCommand(conn, "top -bn1 | grep 'Cpu(s)'"));
// Check memory
checks.push(execCommand(conn, "free -m"));
// Check disk space
checks.push(execCommand(conn, "df -h"));
// Check running processes
checks.push(execCommand(conn, "ps aux | wc -l"));
Promise.all(checks).then(([cpu, memory, disk, processes]) => {
results[server.host] = { cpu, memory, disk, processes };
conn.end();
resolve();
});
})
.connect(server);
});
}
// Send report
generateMonitoringReport(results);
return results;
}
function execCommand(conn, cmd) {
return new Promise(resolve => {
conn.exec(cmd, (err, stream) => {
let output = "";
stream.on("data", data => (output += data.toString()));
stream.on("close", () => resolve(output));
});
});
}
11. RPC (Remote Procedure Call)

What is RPC?
RPC allows a program to execute procedures (functions) on a remote server as if they were local function calls. It abstracts network communication, making distributed computing simpler.
RPC Implementations
gRPC: Google’s high-performance RPC framework using Protocol Buffers JSON-RPC: Simple RPC using JSON XML-RPC: RPC using XML encoding Apache Thrift: Cross-language RPC framework
How RPC Works
- Client calls a procedure as if it’s local
- Client stub marshals parameters
- Request sent over network
- Server stub unmarshals parameters
- Server executes procedure
- Result sent back to client
- Client receives result
Real-World Examples
Example 1: gRPC Microservices
// user_service.proto
syntax = "proto3";
service UserService {
rpc GetUser (UserRequest) returns (UserResponse);
rpc CreateUser (CreateUserRequest) returns (UserResponse);
rpc ListUsers (Empty) returns (stream UserResponse);
}
message UserRequest {
int32 user_id = 1;
}
message CreateUserRequest {
string name = 1;
string email = 2;
}
message UserResponse {
int32 user_id = 1;
string name = 2;
string email = 3;
string created_at = 4;
}
message Empty {}
// Server implementation
const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
const packageDefinition = protoLoader.loadSync("user_service.proto");
const userProto = grpc.loadPackageDefinition(packageDefinition);
// Implement RPC methods
const userService = {
GetUser: (call, callback) => {
const userId = call.request.user_id;
// Query database
db.query("SELECT * FROM users WHERE id = ?", [userId], (err, results) => {
if (err) {
callback({
code: grpc.status.INTERNAL,
details: "Database error",
});
return;
}
const user = results[0];
callback(null, {
user_id: user.id,
name: user.name,
email: user.email,
created_at: user.created_at.toISOString(),
});
});
},
CreateUser: (call, callback) => {
const { name, email } = call.request;
db.query(
"INSERT INTO users (name, email) VALUES (?, ?)",
[name, email],
(err, result) => {
if (err) {
callback({
code: grpc.status.ALREADY_EXISTS,
details: "User already exists",
});
return;
}
callback(null, {
user_id: result.insertId,
name,
email,
created_at: new Date().toISOString(),
});
}
);
},
ListUsers: call => {
// Stream users to client
db.query("SELECT * FROM users", (err, users) => {
users.forEach(user => {
call.write({
user_id: user.id,
name: user.name,
email: user.email,
created_at: user.created_at.toISOString(),
});
});
call.end();
});
},
};
// Start gRPC server
const server = new grpc.Server();
server.addService(userProto.UserService.service, userService);
server.bindAsync(
"0.0.0.0:50051",
grpc.ServerCredentials.createInsecure(),
() => {
console.log("gRPC server running on port 50051");
server.start();
}
);
// Client implementation
const client = new userProto.UserService(
"localhost:50051",
grpc.credentials.createInsecure()
);
// Call remote procedure
client.GetUser({ user_id: 123 }, (err, response) => {
if (err) {
console.error("Error:", err);
return;
}
console.log("User:", response);
// Output: { user_id: 123, name: 'Alice', email: 'alice@example.com', ... }
});
// Create new user
client.CreateUser(
{ name: "Bob", email: "bob@example.com" },
(err, response) => {
console.log("Created user:", response);
}
);
// Stream all users
const call = client.ListUsers({});
call.on("data", user => {
console.log("Received user:", user);
});
call.on("end", () => {
console.log("Finished receiving users");
});
Example 2: JSON-RPC API
// JSON-RPC server
const jayson = require("jayson");
const server = jayson.server({
// Calculate sum
add: function (args, callback) {
const [a, b] = args;
callback(null, a + b);
},
// Get user balance
getBalance: function (args, callback) {
const [userId] = args;
db.query(
"SELECT balance FROM accounts WHERE user_id = ?",
[userId],
(err, results) => {
if (err) {
callback({ code: -32603, message: "Database error" });
return;
}
callback(null, results[0].balance);
}
);
},
// Transfer money
transfer: function (args, callback) {
const [fromUserId, toUserId, amount] = args;
db.beginTransaction(err => {
if (err) return callback(err);
// Debit sender
db.query(
"UPDATE accounts SET balance = balance - ? WHERE user_id = ?",
[amount, fromUserId],
err => {
if (err) {
db.rollback();
return callback(err);
}
// Credit receiver
db.query(
"UPDATE accounts SET balance = balance + ? WHERE user_id = ?",
[amount, toUserId],
err => {
if (err) {
db.rollback();
return callback(err);
}
db.commit(err => {
if (err) {
db.rollback();
return callback(err);
}
callback(null, { success: true, transactionId: generateId() });
});
}
);
}
);
});
},
});
// Start server
server.http().listen(3000, () => {
console.log("JSON-RPC server listening on port 3000");
});
// JSON-RPC client
const jayson = require("jayson");
const client = jayson.client.http("http://localhost:3000");
// Simple RPC call
client.request("add", [5, 3], (err, response) => {
if (err) throw err;
console.log("5 + 3 =", response.result); // 8
});
// Get account balance
client.request("getBalance", [456], (err, response) => {
if (err) throw err;
console.log("Balance:", response.result); // 1250.50
});
// Transfer money
client.request("transfer", [456, 789, 100], (err, response) => {
if (err) throw err;
console.log("Transfer result:", response.result);
// { success: true, transactionId: 'txn_abc123' }
});
// Batch requests
client.request(
[
{ method: "add", params: [10, 20] },
{ method: "getBalance", params: [456] },
{ method: "getBalance", params: [789] },
],
(err, responses) => {
console.log("Results:", responses);
// [30, 1150.50, 200]
}
);
Example 3: Distributed Computing with RPC
// Math computation service
const mathService = {
// Calculate Fibonacci number
fibonacci: function (args, callback) {
const [n] = args;
if (n <= 1) {
return callback(null, n);
}
// For large numbers, distribute computation
if (n > 20) {
// Call other worker nodes
const workers = getAvailableWorkers();
Promise.all([
callWorker(workers[0], "fibonacci", [n - 1]),
callWorker(workers[1], "fibonacci", [n - 2]),
]).then(([fib1, fib2]) => {
callback(null, fib1 + fib2);
});
} else {
// Calculate locally
const result = calculateFibonacci(n);
callback(null, result);
}
},
// Prime number check
isPrime: function (args, callback) {
const [num] = args;
if (num < 2) return callback(null, false);
// Distribute range checking across workers
const workers = getAvailableWorkers();
const rangePerWorker = Math.sqrt(num) / workers.length;
const checks = workers.map((worker, i) => {
const start = 2 + i * rangePerWorker;
const end = start + rangePerWorker;
return callWorker(worker, "checkRange", [num, start, end]);
});
Promise.all(checks).then(results => {
const isPrime = !results.some(r => r === false);
callback(null, isPrime);
});
},
};
Example 4: Microservices Communication
// Order service calls inventory service via RPC
class OrderService {
async createOrder(customerId, items) {
// Call inventory service to check stock
const inventoryClient = new grpc.Client("inventory-service:50051");
for (const item of items) {
const response = await new Promise((resolve, reject) => {
inventoryClient.CheckStock(
{ product_id: item.productId, quantity: item.quantity },
(err, res) => (err ? reject(err) : resolve(res))
);
});
if (!response.available) {
throw new Error(`Product ${item.productId} out of stock`);
}
}
// Call payment service
const paymentClient = new grpc.Client("payment-service:50052");
const payment = await new Promise((resolve, reject) => {
paymentClient.ProcessPayment(
{
customer_id: customerId,
amount: calculateTotal(items),
currency: "USD",
},
(err, res) => (err ? reject(err) : resolve(res))
);
});
if (!payment.success) {
throw new Error("Payment failed");
}
// Call shipping service
const shippingClient = new grpc.Client("shipping-service:50053");
const shipping = await new Promise((resolve, reject) => {
shippingClient.CreateShipment(
{
customer_id: customerId,
items: items,
address: getCustomerAddress(customerId),
},
(err, res) => (err ? reject(err) : resolve(res))
);
});
// Create order record
const order = await db.createOrder({
customerId,
items,
paymentId: payment.transaction_id,
trackingNumber: shipping.tracking_number,
status: "confirmed",
});
return order;
}
}
Example 5: Game Server RPC
// Game server with RPC for player actions
const gameService = {
// Player joins game
joinGame: (call, callback) => {
const { player_id, game_id } = call.request;
const game = games.get(game_id);
if (!game) {
return callback({
code: grpc.status.NOT_FOUND,
details: "Game not found",
});
}
if (game.players.length >= game.maxPlayers) {
return callback({
code: grpc.status.RESOURCE_EXHAUSTED,
details: "Game is full",
});
}
game.players.push(player_id);
callback(null, {
success: true,
game_state: serializeGameState(game),
player_position: game.players.length - 1,
});
},
// Player performs action
playerAction: (call, callback) => {
const { player_id, game_id, action, data } = call.request;
const game = games.get(game_id);
processPlayerAction(game, player_id, action, data);
// Broadcast to other players
broadcastGameUpdate(game_id, {
player_id,
action,
new_state: serializeGameState(game),
});
callback(null, {
success: true,
game_state: serializeGameState(game),
});
},
// Stream game updates to client
streamGameUpdates: call => {
const { game_id } = call.request;
const subscription = subscribeToGameUpdates(game_id, update => {
call.write({
timestamp: Date.now(),
event_type: update.type,
data: JSON.stringify(update.data),
});
});
call.on("cancelled", () => {
unsubscribe(subscription);
});
},
};
Protocol Comparison and Use Cases
When to Use Each Protocol
| Protocol | Best For | Not Suitable For |
|---|---|---|
| HTTP | Web APIs, RESTful services, file downloads | Real-time bidirectional communication |
| WebSocket | Chat apps, live updates, gaming | Simple request-response scenarios |
| WebRTC | Video calls, peer-to-peer file sharing | Text-only communication |
| MQTT | IoT devices, low-bandwidth scenarios | Large file transfers |
| AMQP | Enterprise messaging, guaranteed delivery | Simple pub-sub needs |
| SMTP | Sending emails | Real-time messaging |
| IMAP | Multi-device email access | Single-device usage |
| POP3 | Single-device email download | Multi-device sync |
| FTP | Large file transfers, backups | Secure transfers (use SFTP) |
| SSH | Remote server access, secure tunnels | Public web services |
| RPC | Microservices, distributed systems | Simple client-server apps |
Real-World Architecture Examples
Example 1: Social Media Platform
Frontend App
├─ HTTP/HTTPS: Load static assets, API calls
├─ WebSocket: Real-time notifications, chat
└─ WebRTC: Video/voice calls
Backend Services
├─ HTTP: REST API gateway
├─ gRPC: Internal microservice communication
├─ AMQP: Event queue for async processing
├─ SMTP: Email notifications
└─ Redis Pub/Sub: Real-time message distribution
Example 2: E-commerce System
Customer-Facing
├─ HTTPS: Product browsing, checkout
├─ WebSocket: Live inventory updates, order tracking
└─ SMTP: Order confirmations, shipping updates
Backend Processing
├─ AMQP: Order processing queue
├─ gRPC: Inventory/payment/shipping services
├─ FTP: Supplier data feeds
└─ SSH: Database backups, deployment
Third-Party Integration
├─ HTTP: Payment gateway APIs
└─ SMTP/IMAP: Customer support emails
Example 3: Smart Home System
Devices
├─ MQTT: Sensor data, device commands
└─ WebSocket: Real-time device status
Mobile App
├─ HTTPS: Configuration, user data
├─ WebSocket: Live device updates
└─ Push Notifications: Alerts
Backend
├─ MQTT Broker: Device communication hub
├─ HTTP API: User management, device registration
├─ AMQP: Event processing, automation rules
└─ SMTP: Alert emails
Example 4: Live Streaming Platform
Streaming
├─ WebRTC: Ultra-low latency streaming
├─ HLS over HTTP: Standard video delivery
└─ WebSocket: Chat, reactions
Content Management
├─ HTTPS: Upload videos, metadata
├─ FTP/SFTP: Large file transfers
└─ SSH: Server management
Backend
├─ gRPC: Transcoding services
├─ AMQP: Video processing queue
└─ SMTP: Creator notifications
Security Considerations
Protocol Security Comparison
Secure Protocols:
- HTTPS: Encrypted HTTP with TLS/SSL
- WSS: Encrypted WebSocket
- SFTP: SSH File Transfer Protocol
- SSH: Encrypted remote access
- IMAPS/POP3S: Encrypted email retrieval
- SMTPS: Encrypted email sending
Insecure Protocols (avoid or use with caution):
- HTTP: Plaintext, vulnerable to eavesdropping
- FTP: Credentials sent in plaintext
- Telnet: Unencrypted remote access (use SSH instead)
Best Practices
// Always use encrypted connections
const secureOptions = {
// HTTPS
https: true,
ssl: {
cert: fs.readFileSync("./cert.pem"),
key: fs.readFileSync("./key.pem"),
},
// WebSocket Secure
wsProtocol: "wss://",
// SMTP with TLS
smtpSecure: true,
smtpPort: 465,
// SFTP instead of FTP
protocol: "sftp",
// gRPC with SSL
grpcCredentials: grpc.credentials.createSsl(
fs.readFileSync("./ca.pem"),
fs.readFileSync("./key.pem"),
fs.readFileSync("./cert.pem")
),
};
Performance Optimization
Connection Pooling
// HTTP connection pooling
const http = require("http");
const agent = new http.Agent({
keepAlive: true,
maxSockets: 50,
maxFreeSockets: 10,
timeout: 60000,
});
// Reuse connections
fetch("https://api.example.com/data", { agent });
Message Batching
// Batch MQTT messages
const messageBatch = [];
setInterval(() => {
if (messageBatch.length > 0) {
client.publish("sensors/batch", JSON.stringify(messageBatch));
messageBatch.length = 0;
}
}, 5000);
// Add to batch instead of publishing immediately
function recordSensorData(data) {
messageBatch.push(data);
}
Compression
// HTTP compression
const compression = require("compression");
app.use(compression());
// gRPC compression
const call = client.getData(
{},
{
metadata: { "grpc-encoding": "gzip" },
}
);
Conclusion
Understanding application layer protocols is essential for building modern networked applications. Each protocol serves specific purposes:
- HTTP/HTTPS powers the web and REST APIs
- WebSocket enables real-time bidirectional communication
- WebRTC facilitates peer-to-peer audio/video/data
- MQTT efficiently connects IoT devices
- AMQP provides robust enterprise messaging
- SMTP/IMAP/POP3 handle email communication
- FTP transfers large files
- SSH secures remote access
- RPC simplifies distributed computing
By choosing the right protocol for each use case and following security best practices, you can build scalable, efficient, and secure applications that communicate effectively across networks.
Further Learning
Resources:
- RFC Documents - Official protocol specifications
- MDN Web Docs - WebSocket, WebRTC guides
- gRPC Documentation - RPC implementation
- MQTT.org - MQTT specification and tutorials
- SSH.com - SSH protocol guide
Practice Projects:
- Build a real-time chat application (WebSocket)
- Create an IoT dashboard (MQTT)
- Implement a file sharing service (FTP/SFTP)
- Design a microservices system (gRPC/AMQP)
- Develop a video conferencing tool (WebRTC)
Happy networking!