AQA 7517, section 4.9.4.10
The client-server model
In the client-server model, the work is shared between two kinds of program.
- The client sends a request message to the server.
- The server responds to the request by sending back a response message to the client.
A web browser (client) requests a page, and the web server sends it back. The client starts every exchange, and the server waits and responds. This is the basis of the web and of most Internet services.
WebSockets
The problem with plain HTTP
With HTTP, the client must ask each time, and the server can only answer. For a live application, such as a chat or live scores, the client would have to keep asking ("any new messages?"), which is wasteful and slow, and the server can't send news by itself.
What WebSocket is
The WebSocket protocol gives a full-duplex "socket" connection between a web browser and a server, over TCP.
- The connection is persistent: it stays open, instead of being closed after each request.
- It is full-duplex: both parties can send data at any time, without waiting for a request.
- It starts with a normal HTTP request that asks to "upgrade" to a WebSocket, and after that it carries messages in both directions.
- The WebSocket specification defines an API (application programming interface) that JavaScript in the browser can use.
Why and where it is used
- Real-time applications needing instant updates with low delay.
- Uses: online chat, multiplayer games, live sports scores, stock prices, collaborative editing, and live notifications.
- It reduces the overhead of repeatedly opening connections and sending HTTP headers.
Web CRUD applications and REST
CRUD
Most web applications that use a database do four basic operations, remembered as CRUD:
| Letter | Operation | Meaning |
|---|---|---|
| C | Create | Add new data |
| R | Retrieve | Read existing data |
| U | Update | Change existing data |
| D | Delete | Remove data |
REST
REST (Representational State Transfer) is a style for designing web services that uses the standard HTTP request methods. It lets a browser talk to a database through the server.
REST maps CRUD onto HTTP methods and SQL:
| CRUD | HTTP method | SQL |
|---|---|---|
| Create | POST | INSERT |
| Retrieve | GET | SELECT |
| Update | PUT | UPDATE |
| Delete | DELETE | DELETE |
How a REST application works
- Each resource (such as all the students, or student 7) is identified by its own URL. Requesting that URL, using HTTP, makes the server carry out the matching CRUD operation on the database.
- A REST API (application programming interface) is created and run on the server. It receives HTTP requests and does the matching database operation.
- JavaScript running in the browser (referenced by the HTML file, such as
index.html) calls the API. This lets JavaScript talk to the server through HTTP, and update the page without reloading it. - Data is sent between the server and the web application as JSON or XML.
Example requests for a "students" resource:
| Request | Meaning | SQL it maps to |
|---|---|---|
GET /api/students |
List all students | SELECT * FROM students |
GET /api/students/7 |
Get student 7 | SELECT ... WHERE id = 7 |
POST /api/students |
Add a student | INSERT INTO students ... |
PUT /api/students/7 |
Change student 7 | UPDATE students ... WHERE id = 7 |
DELETE /api/students/7 |
Remove student 7 | DELETE FROM students WHERE id = 7 |
JSON compared with XML
Both formats transmit data between a server and a web application as text.
JSON (JavaScript Object Notation):
{
"name": "Asha",
"year": 12,
"subjects": ["Maths", "Physics"]
}
XML (Extensible Markup Language), holding the same data:
<student>
<name>Asha</name>
<year>12</year>
<subjects>
<subject>Maths</subject>
<subject>Physics</subject>
</subjects>
</student>
JSON compared with XML is:
- easier for a human to read;
- more compact (shorter, with no closing tags, so it uses less bandwidth);
- easier to create;
- easier for computers to parse, and therefore quicker to parse.
XML is still used where its extra features (such as formal schemas and attributes) are needed.
Worked example
Task: a web page shows a list of products and lets the user delete one. Say which HTTP methods and SQL operations are involved.
- Showing the list: the browser's JavaScript sends GET
/api/products. The server runs a SELECT and returns the rows as JSON. - Deleting product 12: JavaScript sends DELETE
/api/products/12. The server runs a DELETE SQL statement and replies that it succeeded. The JavaScript then removes it from the page.
If the page also needed to show live stock changes made by other users, a WebSocket could push the updates to every open page at once.
Exam tip: learn the mapping GET to SELECT, POST to INSERT, PUT to UPDATE, DELETE to DELETE. For WebSocket, the key words are persistent, full-duplex and either side can send at any time. For JSON versus XML, give two points, such as more compact and quicker to parse.
Check yourself
Describe the client-server model
The client sends a request message to the server, and the server responds by sending a response message back to the client.
What is special about the WebSocket protocol, and where is it used?
It gives a persistent, full-duplex connection between a browser and a server, so either side can send data at any time. It is used for real-time applications such as chat, multiplayer games and live scores.
What does CRUD stand for, and how does it map to HTTP and SQL?
Create (POST, INSERT), Retrieve (GET, SELECT), Update (PUT, UPDATE) and Delete (DELETE, DELETE).
Give two advantages of JSON over XML
Any two of: easier for humans to read; more compact; easier to create; easier and quicker for computers to parse.