NodeJS

Socket.IO

It enables realtime, bi-directional communication between web clients and servers. It has two parts: a client-side library that runs in the browser, and a server-side library for Node.js. Both components have a nearly identical API. Like Node.js. it is event-driven.

Multiple Protocols: Socket.IO will use feature detection to decide if the connection will be established with WebSocket, AJAX long polling, Flash, etc. Socket.IO primarily uses the WebSocket protocol. When WebSocket is not available, it will use other available protocols supported by the browser.

Other Cool Features: broadcasting to multiple sockets, storing data associated with each client, and asynchronous I/O.

broadcasting: emit the event from the server to the rest of the users.

Socket.IO is composed of two parts:

  • A server that integrates with (or mounts on) the Node.JS HTTP Server: socket.io
  • A client library that loads on the browser side: socket.io-client

emit event: client -> server
The main idea behind Socket.IO is that you can send and receive any events you want, with any data you want.

Exclusiveness: A Socket.IO implementing server cannot connect to a non-Socket.IO WebSocket client. A Socket.IO implementing client cannot talk to a non-Socket.IO WebSocket or Long Polling Comet server. Socket.IO requires using the Socket.IO libraries on both client and server side.

WebSocket API

reference: https://itnext.io/building-a-node-js-websocket-chat-app-with-socket-io-and-react-473a0686d1e1

a computer communication protocol

establish a persistent connection between server and client

server can send message to client without being requested first.

lower overheads

Pros:

  1. Realtime Communication: In a request-response scenario, there is no way for a server to send data to the client without having the client to request something first. A client would have to continously ask for changes in regular intervals (polling), which is not what we consider realtime.

  2. Less Overhead: HTTP is a stateless protocol, thus the overhead of a HTTP header is added to every single message, which can get quite large in size. Moreover, a HTTP connection will usually only keep alive for a certain amount of requests and will be closed after some time of being idle. Thus connections will quite frequently have to be reestablished, which introduces an initial setup time due to the TCP 3-Way Handshake

  3. Streaming Processing: With WebSockets one can essentially stream binary data of arbitrary size between client and server. Thus it’s well suited for stream processing tasks such as image processing apps, which might stream images or video data back and forth.

  4. server and client can push messages to each other at any given time.

REST

REST, or REpresentational State Transfer, is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other.

  • SEPARATION OF CLIENT AND SERVER: n the REST architectural style, the implementation of the client and the implementation of the server can be done independently without each knowing about the other. By using a REST interface, different clients hit the same REST endpoints, perform the same actions, and receive the same responses. As long as each side knows what format of messages to send to the other, they can be kept modular and separate.
  • Statelessness: Systems that follow the REST paradigm are stateless, meaning that the server does not need to know anything about what state the client is in and vice versa. In this way, both the server and the client can understand any message received, even without seeing previous messages. This constraint of statelessness is enforced through the use of resource. ()

Communication between Client and Server

Make Request

REST requires that a client make a request to the server in order to retrieve or modify data on the server. A request generally consists of:

  • an HTTP verb, which defines what kind of operation to perform
  • a header, which allows the client to pass along information about the request
  • a path to a resource
  • an optional message body containing data

There are 4 basic HTTP verbs we use in requests to interact with resources in a REST system:

  • GET — retrieve a specific resource (by id) or a collection of resources
  • POST — create a new resource
  • PUT — update a specific resource (by id)
  • DELETE — remove a specific resource by id

REST based APIs

built upon HTTP

client requests a page and then server responds (request-respond)

Polling: A client would have to continuously ask for changes in regular intervals

Three.js

results matching ""

    No results matching ""