jakubrohleder / angular-jsonapi

Simple and lightweight, yet powerful ORM for your frontend that seamlessly integrates with your JsonAPI server.
http://jakubrohleder.github.io/angular-jsonapi/
GNU General Public License v3.0
96 stars 34 forks source link

Support for Server Side Events through WebSockets #26

Open jsenecal opened 8 years ago

jsenecal commented 8 years ago

My current implementation sends updates to connected clients when events occur on the server:

Update format:

For a create:

{
  "data": {
    "type": "organization",
    "id": "500"
  },
  "meta": {
    "update_type": "CREATE",
  }
}

For A change:

{
  "data": {
    "type": "organization",
    "id": "500",
    "attributes": {
      "permissions": {
        "change": true,
        "view": true,
        "add": true,
        "delete": true
      },
      "created_at": "2015-09-25T19:59:40.422528Z",
      "modified_at": "2015-09-25T19:59:49.607210Z",
      "name": "11111",
      "nickname": null,
      "reference": "111001",
      "notes": null
    },
    "relationships": {
      "children": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/children",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/children"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "parent": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/parent",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/parent"
        },
        "data": null
      },
      "resellers": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/resellers",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/resellers"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "customers": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/customers",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/customers"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "departments": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/departments",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/departments"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "people": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/people",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/people"
        },
        "data": [
          {
            "type": "person",
            "id": "1162"
          },
          {
            "type": "person",
            "id": "1019"
          },
          {
            "type": "person",
            "id": "1390"
          },
          {
            "type": "person",
            "id": "771"
          },
          {
            "type": "person",
            "id": "1548"
          }
        ],
        "meta": {
          "count": 5
        }
      },
      "administrators": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/administrators",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/administrators"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "email_domains": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/email_domains",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/emaildomains"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "phone_numbers": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/phone_numbers",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/phonenumbers"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "email_addresses": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/email_addresses",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/emailaddresses"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      },
      "urls": {
        "links": {
          "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/relationships/urls",
          "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500/urls"
        },
        "data": [],
        "meta": {
          "count": 0
        }
      }
    },
    "links": {
      "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/500"
    }
  },
  "meta": {
    "update_type": "UPDATE",
  }
}

For A delete:

{
  "data": {
    "type": "organization",
    "id": "500"
  },
  "meta": {
    "update_type": "DELETE",
  }
}

The reason why the object is not send with the creation is that it gives a notification instead of the actual data - This permits a validation of the required permissions on the server side when client GETs the object(s).

jsenecal commented 8 years ago

The more I think of it, I think that angular-jsonapi should only have hooks to pass the CUD operations to so that the websocket connection could still be used (and/or handled) for other purposes.