npm start
(npm start -- -p 9898 -d 1000
) to start the server.
- HTTP GET
http://localhost:3000/todos
to access current todos - websocket
http://localhost:3000
to subscribe/push events
port
/p
delay
/d
- minimum delay of each response/broadcast (milliseconds)
Start with following code on client side:
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
const socket = io('http://localhost:3000')
socket.on('cmd', (data) => {
console.log('received', data)
})
Each command requires to have a type
specified, as well as id
of the item that is being modified.
Responses are being sent only to the direct sender (committing the change). The response object contains the todo entity for all 3 operations (add, delete, update).
If a request gets committed, it's also broadcasted to all other clients. The broadcasted event is the same as the request.
{
"type": "ADD" | "DELETE" | "UPDATE",
"payload": {
"id": GUID,
"title": STRING,
"completed": BOOLEAN,
}
}
Adds a todo. The server assigns the id
.
{
"type": "ADD",
"payload": {
"title": STRING
}
}
{
"type": "ADD",
"payload": {
"title": "do some cool stuff",
}
}
Deletes a todo.
{
"type": "DELETE",
"payload": {
"id": GUID
}
}
{
"type": "DELETE",
"payload": {
"id": "45a70dfd-d6f7-4c80-a493-f6bbf7005d96"
}
}
Can be used to either change todo title or mark as (in)complete, depending on which attributes of the todo object are being changed
{
"type": "UPDATE",
"payload": {
"id": GUID,
"data": {
"title": STRING,
"completed": BOOLEAN
}
}
}
{
"type": "UPDATE",
"payload": {
"id": "45a70dfd-d6f7-4c80-a493-f6bbf7005d96",
"data": {
"completed": true
}
}
}
{
"type": "UPDATE",
"payload": {
"id": "45a70dfd-d6f7-4c80-a493-f6bbf7005d96",
"data": {
"title": "Go do some jogging"
}
}
}