DEV Community

Cover image for Dive into APIs: REST, SOAP, GraphQL & WebSocket
Gazi Nahian
Gazi Nahian

Posted on

1 1 1

Dive into APIs: REST, SOAP, GraphQL & WebSocket

APIs are the magic bridges connecting apps and data! But which one should you use? Let's break it down with code examples that’ll make you go, β€œOh, that’s cool!” πŸ˜ƒ


1️⃣ REST API – The Classic

A widely used web API architecture that operates over HTTP and uses URLs for resource identification. It primarily relies on standard HTTP methods like GET, POST, PUT, DELETE.

πŸ“ Quick REST API (Node.js & Express)

const express = require('express');
const app = express();
const books = [{ id: 1, title: "\"JavaScript Magic\" }];"

app.get('/books', (req, res) => res.json(books));
app.listen(8080, () => console.log('πŸ“‘ REST API running on port 8080!'));

Enter fullscreen mode Exit fullscreen mode

2️⃣ SOAP API – Old but Gold πŸ’Ž

SOAP is a protocol for exchanging structured information, often used in enterprise applications. It uses XML for messages and enforces a strict structure, making it secure and reliable.

πŸ“ Sample SOAP Request

<soapenv:Envelope>
  <soapenv:Body>
    <GetBook>
      <ID>1</ID>
    </GetBook>
  </soapenv:Body>
</soapenv:Envelope>

Enter fullscreen mode Exit fullscreen mode

3️⃣ GraphQL – The Customizer πŸ› οΈ

A query language for APIs that allows clients to request specific data, avoiding over-fetching. Clients define the structure of the response, making it flexible and efficient.

πŸ“ Query Example

{
  book(id: "1") {
    title
    author
  }
}

Enter fullscreen mode Exit fullscreen mode

πŸ“ Tiny GraphQL API (Node.js)

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Book { id: ID, title: String }
  type Query { books: [Book] }
`;

const resolvers = {
  Query: { books: () => [{ id: 1, title: "JavaScript Magic" }] }
};

new ApolloServer({ typeDefs, resolvers }).listen(4000, () =>
  console.log("⚑ GraphQL API running on port 4000!")
);

Enter fullscreen mode Exit fullscreen mode

4️⃣ WebSocket – Real-time Magic! 🎩✨

Enables real-time, two-way communication between the client and server over a persistent connection. Commonly used in chat apps, gaming, and live notifications.

πŸ“ WebSocket Server (Node.js)

const WebSocket = require('ws');
const server = new WebSocket.Server({ port: 8080 });

server.on('connection', socket => {
  socket.send('πŸŽ‰ Welcome to WebSocket!');
});

Enter fullscreen mode Exit fullscreen mode

πŸ“ WebSocket Client

const socket = new WebSocket("ws://localhost:8080");
socket.onmessage = event => console.log("πŸ“© Message received:", event.data);

Enter fullscreen mode Exit fullscreen mode

🎯 Final Thoughts

Each API has its superpowers:

  • REST – Simple & scalable πŸ—οΈ
  • SOAP – Secure & reliable πŸ”’
  • GraphQL – Flexible & efficient πŸŽ›οΈ
  • WebSocket – Real-time & interactive ⚑

Choose the right tool for the job! πŸ”₯

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs