Client-Server is a common architecture pattern in software systems. It is a distributed system architecture that partitions tasks or workloads between the providers of a resource or service, called servers, and service requesters, called clients. There are different patterns in which client and server can communicate with each other.

Request-Response

  • The client sends a request to the server and waits for a response.
  • The server processes the request and sends a response back to the client.
  • It is a synchronous communication pattern.
  • It is used in HTTP, RPC, and other synchronous communication protocols.
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Request
    Server->>Client: Response

Short Polling

  • The client sends a request to the server at regular intervals.
  • The server responds with new data if available.
  • It is a synchronous communication pattern with a short interval.
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Request
    Server->>Client: Response ( no data )
    Note left of Client: Wait for some time
    Client->>Server: Request
    Server->>Client: Response ( data ready )

Long Polling

  • The client sends a request to the server and waits for a response.
  • The server holds the request until new data is available or a timeout occurs.
  • It is a synchronous communication pattern with a long timeout.
sequenceDiagram
    participant Client
    participant Server
    Client->>+Server: Request
    Note right of Server: Wait Until data is ready
    Server->>-Client: Response ( delayed )

WebSockets

  • The client and server establish a persistent, two way communication channel.
  • It is used for real-time communication and updates.
  • It is particularly useful for chat applications, online gaming, and financial trading platforms.
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Establish Connection
    Server->>Client: Establish Connection
    Client->>Server: Send Message
    Server->>Client: Send Message

Server-Sent Events

  • The server sends updates to the client over a single HTTP connection.
  • Client sends a request and the server uses the response to send updates.
  • Server will not end the response, and will keep sending updates over the established connection.
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Establish Connection
    Server->>Client: Send Update
    Server->>Client: Send Update
    Server->>Client: Send Update

Publish-Subscribe

  • The client subscribes to a topic or event on the server.
  • The server publishes messages to the subscribed clients when an event occurs.
  • It’s similar to WebSockets, but the server can send messages to multiple clients and the messages are partitioned by topics.
  • It is an asynchronous communication pattern.
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Subscribe ( establish a connection )
    Server->>Client: Publish ( through the connection )

Request-Callback

  • The client sends a request to the server and provides a callback function or URL.
  • The server processes the request and calls the callback function or URL on the client.
  • It is an asynchronous communication pattern.
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Request ( with callback info )
    Server->>Client: Invoke the Callback