發(fā)生在IPFS上有趣的項(xiàng)目
ipfs is awesome
最近的研究重點(diǎn)在IPFS上敛纲,作為一個(gè)基于BitSwap和Merkle DAG的系統(tǒng),IPFS在不斷完善自身的技術(shù)棧。ipfs_research 中有許多很有想象力的新的協(xié)議婴噩。在這些新的協(xié)議的啟發(fā)下荠藤,我們能在IPFS上開(kāi)發(fā)出更多有意思的產(chǎn)品瞳秽。
1. 為什么需要 pubsub?
參考文獻(xiàn)https://ipfs.io/blog/25-pubsub/
消息的發(fā)布與訂閱Publish-Subscribe, 簡(jiǎn)稱為‘pubsub’, 是一種在大型網(wǎng)絡(luò)中被用于處理事件的模式熏迹。發(fā)布者(‘Publishers’ )按照主題發(fā)送信息或內(nèi)容辑甜;接收者( ‘subscribers’)接受他們感興趣的主題衰絮。 信息的傳輸不需要發(fā)布者和接受者間存在直連。這樣一來(lái)可以得到更大的網(wǎng)絡(luò)擴(kuò)展性和靈活性磷醋。
有些文檔共享編輯的應(yīng)用猫牡、動(dòng)態(tài)內(nèi)容的網(wǎng)站、聊天應(yīng)用邓线、游戲淌友、持續(xù)演化的數(shù)據(jù)集和用戶發(fā)布信息的webservice都需要用到消息的發(fā)布訂閱。它使得在數(shù)據(jù)中心骇陈、局域網(wǎng)震庭、大型P2P應(yīng)用上使用IPFS變得更快速。在不遠(yuǎn)的將來(lái)你雌,IPNS記錄可以通過(guò)pubsub發(fā)布器联,使得IPNS記錄的更新變得更快。節(jié)點(diǎn)間可以通過(guò)pubsub 追蹤一個(gè) merkle-linked global log的頭信息婿崭。
Demo
- 以使用pubsub模式啟動(dòng)ipfs
<pre name="b88d" id="b88d" class="graf graf--pre graf-after--li" style="overflow: auto; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 16px; margin: 43px 0px 0px; background: rgba(0, 0, 0, 0.05); padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.84); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ipfs daemon — enable-pubsub-experiment</pre>
- 訂閱“foo”主題
<pre name="5526" id="5526" class="graf graf--pre graf-after--li" style="overflow: auto; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 16px; margin: 43px 0px 0px; background: rgba(0, 0, 0, 0.05); padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.84); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ipfs pubsub sub foo</pre>
- 在另一個(gè)窗口發(fā)布一條消息拨拓,在另一個(gè)窗口可以顯示“hello world”
<pre name="a638" id="a638" class="graf graf--pre graf-after--li" style="overflow: auto; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 16px; margin: 43px 0px 0px; background: rgba(0, 0, 0, 0.05); padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.84); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ipfs pubsub pub foo “hello world”</pre>
- 查看所有的訂閱者
<pre name="8ce4" id="8ce4" class="graf graf--pre graf-after--li" style="overflow: auto; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 16px; margin: 43px 0px 0px; background: rgba(0, 0, 0, 0.05); padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.84); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ipfs pubsub peers</pre>
- 查看所有的主題
<pre name="1d87" id="1d87" class="graf graf--pre graf-after--li" style="overflow: auto; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 16px; margin: 43px 0px 0px; background: rgba(0, 0, 0, 0.05); padding: 20px; white-space: pre-wrap; color: rgba(0, 0, 0, 0.84); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ipfs pubsub ls</pre>
2. OrbitDB
orbit-db是一個(gè)去服務(wù)器的,分布式的P2P數(shù)據(jù)庫(kù)逛球。 orbit-db將數(shù)據(jù)存儲(chǔ)在IPFS上千元,使用IPFS Pubsub自動(dòng)同步各個(gè)節(jié)點(diǎn)的數(shù)據(jù)苫昌。 這是一個(gè)能夠保證最終一致的數(shù)據(jù)庫(kù)颤绕,它使用CRDT進(jìn)行無(wú)沖突數(shù)據(jù)庫(kù)合并幸海,使orbit-db成為offline-first應(yīng)用程序的絕佳選擇。
案例-orbit
Orbit是一個(gè)P2P的聊天軟件奥务。
Devcon2上的介紹視頻: Orbit Distributed, Real Time Web3 Apps with IPFS and Ethereum
<figure name="636e" id="636e" class="graf graf--figure graf-after--p" style="display: block; margin: 43px 0px 0px; position: relative; clear: both; outline: 0px; box-sizing: border-box; user-select: auto; z-index: 100; color: rgba(0, 0, 0, 0.84); font-family: medium-content-sans-serif-font, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; font-size: 20px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
<canvas class="progressiveMedia-canvas js-progressiveMedia-canvas" width="75" height="40" style="display: block; vertical-align: baseline; position: absolute; top: 0px; left: 0px; width: 700px; height: 394.797px; margin: auto; box-sizing: border-box; visibility: hidden; opacity: 0; backface-visibility: hidden; transition: visibility 0s linear 0.5s, opacity 0.1s 0.4s;"></canvas>
[圖片上傳失敗...(image-a34ad7-1522941041721)]
</figure>
線上測(cè)試環(huán)境:https://orbit.chat/#/loading
Orbit在IPFS上開(kāi)發(fā)出了P2P數(shù)據(jù)庫(kù)OrbitDB物独。
Orbit的Git項(xiàng)目地址:https://github.com/orbitdb
安裝
當(dāng)Nodejs版本在8.0之上,
npm install -g orbit-db-cli
測(cè)試是否安裝成功
orbitdb help
應(yīng)出現(xiàn)以下內(nèi)容:
_ _ _ _ _ | | (_) | | | | ___ _ __| |__ _| |_ __| | |__ / _ \| '__| '_ \| | __| / _` | '_ \ | (_) | | | |_) | | |_ | (_| | |_) | \___/|_| |_.__/|_|\__| \__,_|_.__/ Peer-to-Peer Database https://github.com/orbitdb/orbit-dbUsage: orbitdb <command> <database>Commands: add <database> [<data>] Add an entry to an eventlog or feed database. Can be only used on: eventlog|feed create <database> <type> Create a new database. Type can be one of: eventlog|feed|docstore|keyvalue|counter [aliases: new] del <database> <key> Delete an entry from a database. Only valid for data types of: docstore|keyvalue|feed [aliases: delete, remove] demo <name> Runs a sequence of commands as an example [aliases: tour] drop <database> yes Remove a database locally. This doesn't remove data on other nodes that have the removed database replicated. [aliases: destroy] get <database> [<search>] Query the database. [aliases: query, search] id Show information about current orbit-db id import <file> <database> <schema> Import a CSV file to a document database [aliases: csv] inc <database> [<increment>] Increase the value of a counter database. Default increment is 1\. [aliases: increase] info <database> Show information about a database [aliases: status] put <database> <document> Add a document to a document database replicate <database> Replicate a database with peers. set <database> <key> <value> Set a value of a key in KeyValue database version Show information about current orbit-dbOptions: -h, --help Show help [boolean]
測(cè)試數(shù)據(jù)庫(kù)存儲(chǔ)日志的特性:
- 創(chuàng)建一個(gè)feed用于存儲(chǔ)log信息
orbitdb create hello feed/orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/hello
- 插入一條日志
orbitdb add /orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/hello "world"Added QmSwYZheHVa3eWf83XwnWNJtjGG7EWjiWTaTKLeFozVRnz
- 讀取feed內(nèi)容
$ orbitdb get /orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/hello"world"
- 刪除某一條日志
$ orbitdb del /orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/hello QmSwYZheHVa3eWf83XwnWNJtjGG7EWjiWTaTKLeFozVRnzDeleted QmSwYZheHVa3eWf83XwnWNJtjGG7EWjiWTaTKLeFozVRnz
- 得到feed所有內(nèi)容
orbitdb get /orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/helloDatabase '/orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/hello' is empty!
- 獲取feed所有信息
orbitdb info hello/orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/hello> Type: feed> Owner: QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1> Data file: ./orbitdb/QmfSUsdr34iGio68eMezDzZLCKZTnbsxNJgiNipimZtpi1/hello.orbitdb> Entries: 0> Oplog length: 2
參考文獻(xiàn):
https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type