Go to file
Bevan Hunt 1542daa71e add post create user 2020-01-05 17:50:38 -08:00
example Rewrite (#1) 2020-01-04 01:03:18 -08:00
src add post create user 2020-01-05 17:50:38 -08:00
.gitignore delete tmp 2019-12-31 00:41:16 -08:00
Cargo.lock add post create user 2020-01-05 17:50:38 -08:00
Cargo.toml add post create user 2020-01-05 17:50:38 -08:00
LICENSE move files 2019-12-31 00:46:48 -08:00
Makefile add audit endpoint and fix form submission 2019-12-31 16:05:48 -08:00
Procfile make library 2019-12-31 12:29:09 -08:00
README.md update readme 2020-01-04 15:24:30 -08:00

README.md

Broker - Real-time Zero-Code API Server

crates.io

Purpose

The purpose of this library is to be your real-time zero-code API server.

Broker is a SSE message broker that requires you write no backend code to have a full real-time API.

Broker is born from the need that rather than building a complex REST API with web-sockets and a SQL database to provide reactive web forms (like for React) there must be a simpler way.

Broker follows an insert-only/publish/subscribe paradigm rather than a REST CRUD paradigm.

How it works

In Broker you insert an event and its data via a JSON POST request (/insert) with a timestamp. Broker publishes the event when the timestamp is reached to the event stream via SSE (/events). Broker keeps all versions in its database that can be viewed in a GET request (/collection/{event}). Broker can also cancel future events in a GET request (cancel/{uuid}).

When the client first subscribes to the SSE connection (/events) all the latest events and data is sent to the client. Combined with sending the latest event via SSE when subscribed negates the necessity to do any GET API requests in the lifecycle of an event.

The side-effect of this system is that the latest event is the schema. Old events are saved in the database and are not changed but the latest event is the schema for the front-end. This is pure NoSQL as the backend is agnostic to the event data.

API

/events

  • connect your sse-client to this endpoint

/insert

  • POST JSON to insert an event
{"event":{...}, "published": false, "timestamp":{...}, "data":{...}}
  • where {...} is for the event a string, timestamp is the epoch unix timestamp when you want the event to become the current event, and data is any JSON you want

/collection/{event}

  • do a GET request where {event} is the name of the event you want the event log

/cancel/{uuid}

  • do a GET request where (uuid) is the event uuid you want to cancel - this will mark the event published and it will not be published to the event stream - the event uuid can be obtained from the response from /insert or from /collection/{event}.

Features

  • Very performant with a low memory footprint
  • Real-time Event Stream via SSE
  • CORS support
  • Handles SSE client timeouts
  • Handles future events via Epoch UNIX timestamp
  • Stateful immutable event persistence
  • Insert event via JSON POST request
  • Sync latest events on SSE client connection
  • Event log via GET request
  • Event cancellation via GET request

Use

use broker::{broker_run};

#[actix_rt::main]
async fn main() -> std::result::Result<(), std::io::Error> {
    broker_run("*".to_owned()).await
}
  • the only param is the origin you want to allow - wildcard for all
  • the PORT needs to passed in as an environment variable
  • the file database saves to ./tmp of the project root

View Example

Run Example

  • make
  • make client

Demo

  • https://broker-demo.apibill.me

  • Real-time events across all connected clients: Open the demo in two browser windows watching both. Type in the name field in one and both browser windows will display the name you typed.

  • Sync latest events on client connection: Refresh one of the browsers and the name will be still be the current name and not blank.

Under the Hood

Inspiration