hub.js
Observable data structures, over the network
What is it not?
- Its not a database
- Its not a query language
- Its not persistent storage
What is it?
- Software to help scale a realtime back end to millions of users — without adding any extra worries for developers
- Make an app for users as simple as creating a prototype
- Use the server to offload most of your client side cpu
Why
There is firebase right?
- better subscriptions, “observable deep queries”
- conflict resolution
- flexible
- completely open source — can be hosted on now or anywhere else
- use as a hub for all your integrations — custom AND standard
What does it do?
- Create realtime branches of data
- Gives sandboxes for your application to store data in
- Executes queries
- Sends diffs, keeps track of change
- Integrates apis
- Supports references (serliazable over the network)
- Performs!
- Can be a server
- Can be a client
- Or both
- Runs on the browser
- Can be used as a state store (like redux but simple)
- Reconnects
- Server side sessions
Getting started
npm i hub.js
const Hub = require('hub.js')
const hub = Hub()
.listen(80)
.connect('ws://someurl.com')
Data structure
hub.js uses a data structure modelled to closely resemble plain js objects
Elements can be values and objects at the same time, all element are observable
const hub = Hub({
something: 'hello'
})
hub.set({
something: {
field: 'some field'
}
})
hub.get('something').on(() => {
console.log('fires on change!')
})
hub.set({
something: {
on: {
data: () => {}
}
}
})
console.log(hub.serialize())
References
const hub = Hub({
something: 'hello'
})
hub.set({
thing: hub.get('something')
})
hub.thing.on(() => {
console.log('hello')
})
hub.something.set('bye')
hub.set({
bla: [ '@', 'root', 'other']
})
hub.set({
other: 'thing'
})
Subscriptions
Basic
A simple subscription
client.subscribe(true), (target, type) => {
console.log('update!', target, type)
})
Setting on the server
server.set('hello!')
A shallow subscription
client.subscribe('shallow'), (target, type) => {
console.log('update!', target, type)
})
Any
A simple subscription
client.subscribe({
$any: { title: true }
}, (target, type) => {
console.log('update!', target, type)
})
A complex subscription
client.subscribe({
$any: {
$keys: keys => keys.slice(0, 5),
title: true
}
}, (target, type) => {
console.log('update!', target, type)
})
A complex subscription with sort
client.subscribe({
$any: {
$keys: (keys, state) => keys.sort((a, b) =>
state.get([ a, 'count' ], 0).compute() >
state.get([ b, 'count' ], 0).compute()
).slice(0, 5),
title: true
}
}, (target, type) => {
console.log('update!', target, type)
})
Switch
Switches are probably the most powerful concept in supported in the subscription model, allowing you to branch subscriptions based on certain conditions
client.subscribe({
$any: {
kind: {
$switch: state => {
if (state.compute() === 'dog') {
return {
diet: true
}
} else {
title: true
}
}
}
}
}, (target, type) => {
console.log('update!', target, type)
})