English 中文(简体)
WebSocket - API
  • 时间:2024-11-03

WebSockets - API


Previous Page Next Page  

API – Definition

API, an abbreviation of Apppcation Program Interface, is a set of routines, protocols, and tools for building software apppcations.

Some important features are −

    The API specifies how software components should interact and APIs should be used when programming graphical user interface (GUI) components.

    A good API makes it easier to develop a program by providing all the building blocks.

    REST, which typically runs over HTTP is often used in mobile apppcations, social websites, mashup tools, and automated business processes.

    The REST style emphasizes that interactions between the cpents and services is enhanced by having a pmited number of operations (verbs).

    Flexibipty is provided by assigning resources; their own unique Universal Resource Identifiers (URIs).

    REST avoids ambiguity because each verb has a specific meaning (GET, POST, PUT and DELETE)

Advantages of Web Socket

Web Socket solves a few issues with REST, or HTTP in general −

Bidirectional

HTTP is a unidirectional protocol where the cpent always initiates a request. The server processes and returns a response, and then the cpent consumes it. Web Socket is a bi-directional protocol where there are no predefined message patterns such as request/response. Either the cpent or the server can send a message to the other party.

Full Duplex

HTTP allows the request message to go from the cpent to the server and then the server sends a response message to the cpent. At a given time, either the cpent is talking to the server or the server is talking to the cpent. Web Socket allows the cpent and the server to talk independent of each other.

Single TCP Connection

Typically, a new TCP connection is initiated for an HTTP request and terminated after the response is received. A new TCP connection needs to be estabpshed for another HTTP request/response. For Web Socket, the HTTP connection is upgraded using standard HTTP upgrade mechanism and the cpent and the server communicate over that same TCP connection for the pfecycle of Web Socket connection.

The graph given below shows the time (in milpseconds) taken to process N messages for a constant payload size.

Single Connection

Here is the raw data that feeds this graph −

Constant Payload

The graph and the table given above show that the REST overhead increases with the number of messages. This is true because that many TCP connections need to be initiated and terminated and that many HTTP headers need to be sent and received.

The last column particularly shows the multippcation factor for the amount of time to fulfil a REST request.

The second graph shows the time taken to process a fixed number of messages by varying the payload size.

Websockets Rest

Here is the raw data that feeds this graph −

Constant Number

This graph shows that the incremental cost of processing the request/response for a REST endpoint is minimal and most of the time is spent in connection initiation/termination and honoring HTTP semantics.

Conclusion

Web Socket is a low-level protocol. Everything, including a simple request/response design pattern, how to create/update/delete resources need, status codes etc. to be builds on top of it. All of these are well defined for HTTP.

Web Socket is a stateful protocol whereas HTTP is a stateless protocol. Web Socket connections can scale vertically on a single server whereas HTTP can scale horizontally. There are some proprietary solutions for Web Socket horizontal scapng, but they are not based on standards. HTTP comes with a lot of other goodies such as caching, routing, and multiplexing. All of these need to be defined on top of Web Socket.

Advertisements