Emit data to stream
The role of Bloomr is to forward data you're interested in to a stream of events, to which you can subscribe using your prefered method from your front or your server.
To do that, you scripts will make use of the emit()
function. Every emit()
call will result in a message being forwarded to you.
every emit()
will be forwarded, event if your script threw an execption down the line.
It takes as argument:
- The payload to send (any object serializable to json)
- Some optional emit config
onTx(tx => {
emit({
matchedHash: tx.hash,
value: tx.value,
});
})
Channels
Every script has a "default channel", and any number of channels you might need.
When calling emit()
with only one argument, the message is published to the default channel.
If you need to emit to channels, pass an EmitOptions
as second argument (see runtime types).
For instance, this will publish both to the default channel, and to a subchannel when a transaction has a value.
onTx(tx => {
emit({h: tx.hash, val: tx.value}, tx.value ? {
channel: 'txs with value',
} : undefined)
})
In this example, it when the transaction has a value, the message will be emitted on BOTH default channel and txs with value
channel.
If you enabled storage of emitted messages, that will double your storage cost.
If you dont want to emit on the default channel, use:
emit(myPayload, {
channel: {
name: 'txs with value',
skipDefaultChannel: true,
},
})
})
For instance, suppose that you have a script tracking every UniswapV3 liquidity pool (see UniswapV3 example).
Your script emits every price change for all the tracked liquidity pools (which is a lot of events), but if you want to only subscribe to a single liquidity pool, you could emit events in different substreams for each liquidity pool.
Doing so, you will be able to subscribe at will either to all LPs (from a server, for instance), or to a single one (to display a real-time graph from your front-end via a web socket, for instance)