Skip to content

ducin-public/todomvc-service

Repository files navigation

Start

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

CLI parameters

  • port / p
  • delay / d - minimum delay of each response/broadcast (milliseconds)

Client connection

Start with following code on client side:

markup

<script src="http://localhost:3000/socket.io/socket.io.js"></script>

client logic

const socket = io('http://localhost:3000')

socket.on('cmd', (data) => {
	console.log('received', data)
})

Commands

Each command requires to have a type specified, as well as id of the item that is being modified.

Responses

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).

Broadcasts

If a request gets committed, it's also broadcasted to all other clients. The broadcasted event is the same as the request.

schema

{
  "type": "ADD" | "DELETE" | "UPDATE",
  "payload": {
    "id": GUID,
    "title": STRING,
    "completed": BOOLEAN,
  }
}

ADD

Adds a todo. The server assigns the id.

schema

{
  "type": "ADD",
  "payload": {
    "title": STRING
  }
}

example

{
  "type": "ADD",
  "payload": {
    "title": "do some cool stuff",
  }
}

DELETE

Deletes a todo.

schema

{
  "type": "DELETE",
  "payload": {
    "id": GUID
  }
}

example

{
  "type": "DELETE",
  "payload": {
    "id": "45a70dfd-d6f7-4c80-a493-f6bbf7005d96"
  }
}

UPDATE

Can be used to either change todo title or mark as (in)complete, depending on which attributes of the todo object are being changed

schema

{
  "type": "UPDATE",
  "payload": {
    "id": GUID,
    "data": {
      "title": STRING,
      "completed": BOOLEAN
    }
  }
}

example: marking as complete

{
  "type": "UPDATE",
  "payload": {
    "id": "45a70dfd-d6f7-4c80-a493-f6bbf7005d96",
    "data": {
      "completed": true
    }
  }
}

example: marking as complete

{
  "type": "UPDATE",
  "payload": {
    "id": "45a70dfd-d6f7-4c80-a493-f6bbf7005d96",
    "data": {
      "title": "Go do some jogging"
    }
  }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published