Client-Server Model

1. Client-Server Model

The client-server model is a distributed architecture where clients (e.g., applications, web browsers) make requests to servers, which process and return responses. This model enables data and functionality centralization on the server, while clients focus on user interaction and presentation.

Diagram of Client-Server Model:

       +-------------+       Request        +-------------+
       |             |  ----------------->  |             |
       |   Client    |                      |   Server    |
       |             |  <-----------------  |             |
       +-------------+       Response       +-------------+
  • Client: Sends requests to the server (e.g., requesting data, performing an action).
  • Server: Processes requests and returns responses, often involving database operations or business logic.

2. RESTful APIs

REST (Representational State Transfer) is an architectural style for designing networked applications, using HTTP to handle resource-based requests. Each endpoint in a REST API represents a resource (e.g., users, products) and supports actions via HTTP methods.

HTTP Methods in RESTful APIs:

  • GET: Retrieve a resource.
  • POST: Create a new resource.
  • PUT: Update an existing resource.
  • DELETE: Remove a resource.

Example REST API:

GET /users - Retrieves a list of users

POST /users - Creates a new user

PUT /users/1 - Updates user with ID 1

DELETE /users/1 - Deletes user with ID 1

Example Code (Python Flask REST API):

from flask import Flask, jsonify, request

app = Flask(__name__)

users = []

@app.route('/users', methods=['GET'])
def get_users():
    return jsonify(users)

@app.route('/users', methods=['POST'])
def create_user():
    user = request.json
    users.append(user)
    return jsonify(user), 201

if __name__ == '__main__':
    app.run()

3. SOAP (Simple Object Access Protocol)

SOAP is a protocol for building web services that allows applications to communicate using XML-based messages. Unlike REST, SOAP has a stricter set of rules and standards, making it suitable for secure and complex applications.

Key Components of SOAP:

Envelope: Defines the structure of the message.

  • Header: Contains optional information, like authentication.
  • Body: Contains the main message data.
  • Fault: Defines error-handling information.

SOAP Request Example:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://example.com/webservices">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetUserDetails>
         <web:UserID>123</web:UserID>
      </web:GetUserDetails>
   </soapenv:Body>
</soapenv:Envelope>

4. GraphQL

GraphQL is a query language and runtime for APIs that allows clients to request exactly the data they need, avoiding over-fetching and under-fetching issues.

Key Components of GraphQL:

  • Schema: Defines types, queries, and mutations in the GraphQL API.
  • Query: Retrieves data.
  • Mutation: Modifies data.
  • Subscription: Real-time updates (optional).

Example GraphQL Query:

query {
  user(id: "1") {
    name
    email
    posts {
      title
      content
    }
  }
}

Example Schema Definition:

type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post]
}

type Post {
  title: String!
  content: String!
}

type Query {
  user(id: ID!): User
}

type Mutation {
  addUser(name: String!, email: String!): User
}

Track your progress

Mark this subtopic as completed when you finish reading.