I am thinking about buildning a REST API with both websockets and http where I use websockets to tell the client that new data is available or provide the new data to the client directly.
Here are some different ideas of how it could work:
ws = websocket
Idea A:
- David get all users with
GET /users
- Jacob add a user with
POST /users
- A ws message is sent to all clients with info that a new user exist
- David recive a message by ws and calls
GET /users
Idea B:
- David get all users with
GET /users
- David register to get ws updates when a change is done to
/users
- Jacob add a user with
POST /users
- The new user is sent to David by ws
Idea C:
- David get all users with
GET /users
- David register to get ws updates when a change is done to
/users
- Jacob add a user with
POST /users
and it gets the id 4 - David receive the id 4 of the new user by ws
- David get the new user with
GET /users/4
Idea D:
- David get all users with
GET /users
- David register to get ws updates when changes is done to
/users
. - Jacob add a user with
POST /users
- David receive a ws message that changes is done to
/users
- David get only the delta by calling
GET /users?lastcall='time of step one'
Which alternative is the best and what are the pros and cons?
Is it another better 'Idea E'?
Do we even need to use REST or is ws enought for all data?
Edit
To solve problems with data getting out of sync we could provide the header
"If-Unmodified-Since"
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Unmodified-Since
or "E-Tag"
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
or both with PUT requests.