Go to file
Bevan Hunt 7cb0941b6c add service and update readme 2021-03-21 22:12:39 -07:00
src replace broker-ntp with nippy 2021-03-21 21:25:58 -07:00
.gitignore update ignore file 2021-03-20 02:36:54 -07:00
CHANGELOG.md replace broker-ntp with nippy 2021-03-21 21:25:58 -07:00
Cargo.lock add service and update readme 2021-03-21 22:12:39 -07:00
Cargo.toml add service and update readme 2021-03-21 22:12:39 -07:00
LICENSE rewrite to 6.0.0 2021-03-20 02:25:29 -07:00
README.md add service and update readme 2021-03-21 22:12:39 -07:00
broker.service add service and update readme 2021-03-21 22:12:39 -07:00

README.md

Broker - Real-time BaaS (Backend as a Service)

crates.io

Purpose

The purpose of this service is to be your real-time BaaS (Backend as a Service).

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.

Features

  • Very performant with almost no CPU and memory usage
  • Under 500 lines of code
  • Secure Real-time Event Stream via SSE - requires the use of broker-client
  • Supports CORS
  • Supports SSL - full end-to-end encryption
  • Provides user authentication with JWTs with stored Argon2 passwords
  • Uses Global NTP servers and doesn't rely on your local server time
  • Insert event via JSON POST request
  • Sync latest events on SSE client connection

How it works

In Broker you create a user, login, then insert an event with its data. Broker then publishes the event via SSE.

When the client first subscribes to the SSE connection 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. This is pure NoSQL as the backend is agnostic to the event data.

Recommeded Services/Libraries to use with Broker

Broker FAQ

  • Why compete against Parse Server and Firebase?

Firebase is not open-source, is not free, and has complicated pricing. Parse Server doesn't have real-time features and is about 30,000 LOC of JS.

  • Will broker work with mobile apps?

Yes with React Native. There may be native 3rd party libraries for SSE that work. In the future official libraries may be made available for native platforms.

Use

Step 1 - create a user

POST /users 
  • public endpoint
  • POST JSON to create a user
{"username":{...}, "password":{...}}
  • where {...} is for username is a string and password is a string

will return 200 or 500 or 400

For JWT Auth: Step 2 - login with the user

POST /login 
  • public endpoint
  • POST JSON to login
{"username":{...}, "password":{...}}
  • where {...} is for username is a string and password is a string

will return

{"jwt":{...}}
  • where {...} is a JWT (string)

Step 3 - connect to SSE

GET /sse
  • authenticated endpoint (Authorization: Bearer {jwt})
  • connect your sse-client to this endpoint using broker-client
  • note: broker-client uses fetch as eventsource doesn't support headers

Step 4 - insert an event

POST /insert 
  • authenticated endpoint (Authorization: Bearer {jwt})
  • POST JSON to insert an event
{"event":{...}, "data":{...}}
  • where {...} is for the event a string and data is any JSON you want

will return: 200 or 500 or 400 or 401

Install

cargo install broker

  • the origin can be passed in as a flag - default *
  • the port can be passed in as a flag - default 8080
  • the jwt_expiry (for jwts) can be passed in as a flag - default 86400
  • the jwt_secret (for jwts) should be passed in as a flag - default secret
  • the secure can be passed in as a flag (true or false) - default false
  • the key_path can be passed in as a flag if connection https - default ./broker.rsa
  • the cert_path can be passed in as a flag if connection https - default ./broker.pem
  • the db can be passed in as a flag where the embedded database will be saved - default tmp
  • example: ./broker --db="tmp" --port="443" --secure="true" --origin="*" --jwt_expiry="86400" --jwt_secret="secret" --key_path="broker.rsa" --cert_path="broker.pem"

Service

There is an example systemctl service for Ubuntu called broker.service in the code

TechStack

Inspiration

Migrations

  • from 5.0 to 6.0: is a full rewrite - there is no upgrade path from 5.0 to 6.0
  • from 4.0 to 5.0: multi-tenancy has been added and sled has been upgraded - there is no upgrade path from 4.0 to 5.0
  • from 3.0 to 4.0: the sse endpoint now returns all events with all collections with the latest collection event rather than just the latest event data for all event types
  • from 2.0 to 3.0: the sse endpoint is now secure and requires the use of the broker-client library
  • from 1.0 to 2.0: the optional API endpoints URLs have been changed but have the same functionality