Asynchronous APIs
-
JavaScript Ajax
introduced background browser-server comms over HTTP.
- But it is still basically "pull",
i.e. the REST
"request/response"
model.
- Some applications need to constantly "push" data to the client
asynchronously.
-
There was a need for a true, two-way,
push-and-pull model.
True browser-server two-way comms (push-pull).
WebSockets with Node
A typical use of WebSockets is with
Node.js,
and JS on client and server, as follows:
- Node.js installed on server.
- Websocket JS library like
Socket.IO
installed on server.
Also has a client-side JS part.
- Node runs server-side JS to set up the Node server:
node server.js
- server.js starts a http server listening at some new port, say port 3000.
- server.js sets up socket connection handling on that Node server.
- Client-side JS
makes a Websocket connection to something like:
https://host:3000
- Client can then transmit data to server.
Server can transmit that to other clients (on this socket).
Clients may be sent new push data from server at any time.
- Multiple clients can talk to each other through server.
- Online chat in browser. Multi-player games in browser.
Websockets demos
Live Websockets demos on
Ancient Brain.
Click to run World:
Websocket buttons at
Ancient Brain.
Click to run World:
Websocket chat at
Ancient Brain.
Click to run World:
Websockets boxes at
Ancient Brain.