MultiChain streams enable a blockchain to be used as a general purpose append-only database, with the blockchain providing timestamping, notarization and immutability. A MultiChain blockchain can contain any number of streams, where the data published in every stream is stored by every node. If a node chooses to subscribe to a stream, it will index that stream’s contents to enable efficient retrieval in various ways.
Each stream is an ordered list of items, in which each item has the following characteristics:
- One or more
publishers
who have digitally signed that item. - A
key
between 0 and 256 bytes in length. - Some
data
, which can reach many megabytes in size. - Information about the item’s transaction and block, including its
txid
,blockhash
,blocktime
, and so on.
Like native assets, MultiChain streams can be referred to in any of three ways:
- An optional stream
name
, chosen at the time of stream creation. If used, the name must be unique on a blockchain, between both assets and streams. Stream names are stored as UTF-8 encoded strings up to 32 bytes in size and are case insensitive. - A
createtxid
, containing the txid of the transaction in which the stream was created. - A
streamref
which encodes the block number and byte offset of the stream creation transaction, along with the first two bytes of its txid.
If root-stream-name
in the blockchain parameters is a non-empty string, it defines a stream which is created with the blockchain and can be written to immediately. The root stream’s createtxid
is the txid of the coinbase of the genesis block, and its streamref
is 0-0-0
.
Create a stream, which can be used for general data storage and retrieval. On the first server:
create stream stream1 false
The false
means the stream can only be written to by those with explicit permissions. Let’s see its permissions:
listpermissions stream1.*
So for now, only the first server has the ability to write to the stream, as well as administrate it. Let’s publish something to it, with key key1
:
publish stream1 key1 73747265616d2064617461
The txid of the stream item is returned. Now let’s see that the stream is visible on another node. On the second server:
liststreams
(The root
stream was in the blockchain by default.) Now we want the second server to subscribe to the stream, then view its contents:
subscribe stream1
liststreamitems stream1
Now we want the second server to be allowed to publish to the stream. On the first server:
grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV send
grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV stream1.write
Note that the address needs both general send permissions for the blockchain, as well as permission to write to this specific stream. Now let’s publish a couple of items on the second server:
publish stream1 key1 736f6d65206f746865722064617461
publish stream1 key2 53747265616d732052756c6521
Now let’s query the stream’s contents in many different ways. Back on the first server:
subscribe stream1
liststreamitems stream1
(should show 3 items)
liststreamkeys stream1
(2 keys)
liststreamkeyitems stream1 key1
(2 items with this key)
liststreampublishers stream1
(2 publishers)
liststreampublisheritems stream1 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV
Snippets:
Streams -1 (node 1 and node 2)
Streams – 2
Reference: https://www.multichain.com/developers/data-streams/
https://www.multichain.com/getting-started/