@parity/light.js
Version:
A high-level reactive JS library optimized for light clients
1,040 lines (432 loc) • 30.4 kB
HTML
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Installation · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="does-it-work-with-a-full-node.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" >
<span>
Getting started
</span>
<ul class="articles">
<li class="chapter active" data-level="1.2.1" data-path="installation.html">
<a href="installation.html">
Installation
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="does-it-work-with-a-full-node.html">
<a href="does-it-work-with-a-full-node.html">
Does this work with a full node?
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" >
<span>
Guides
</span>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../guides/tutorial1-set-up-a-light-client.html">
<a href="../guides/tutorial1-set-up-a-light-client.html">
Tutorial Part 1: Set up a Light Client
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../guides/tutorial2-our-first-rpcobservable.html">
<a href="../guides/tutorial2-our-first-rpcobservable.html">
Tutorial Part 2: Our First RpcObservable
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../guides/tutorial3-manipulating-rpcobservables.html">
<a href="../guides/tutorial3-manipulating-rpcobservables.html">
Tutorial Part 3: Manipulating RpcObservables
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="../guides/tutorial4-send-a-transaction.html">
<a href="../guides/tutorial4-send-a-transaction.html">
Tutorial Part 4: Send a Transaction
</a>
</li>
<li class="chapter " data-level="1.3.5" data-path="../guides/tutorial5-work-with-contracts.html">
<a href="../guides/tutorial5-work-with-contracts.html">
Tutorial Part 5: Work with Contracts
</a>
</li>
<li class="chapter " data-level="1.3.6" data-path="../guides/tutorial6-integrate-with-react.html">
<a href="../guides/tutorial6-integrate-with-react.html">
Tutorial Part 6: Integrate with React
</a>
</li>
<li class="chapter " data-level="1.3.7" data-path="../guides/redux-integration.md">
<span>
Redux Integration
</a>
</li>
<li class="chapter " data-level="1.3.8" data-path="../guides/angular-integration.md">
<span>
Angular Integration
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" >
<span>
Concepts
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../concepts/light-client-development.html">
<a href="../concepts/light-client-development.html">
Light Client Development
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../concepts/reactive-programming.html">
<a href="../concepts/reactive-programming.html">
Reactive Programming
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../concepts/rpc-observables.html">
<a href="../concepts/rpc-observables.html">
RpcObservables
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="../concepts/rpc-observables-properties.html">
<a href="../concepts/rpc-observables-properties.html">
RpcObservables Properties
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" >
<span>
API
</span>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../api/modules/_api_.html">
<a href="../api/modules/_api_.html">
api
</a>
</li>
<li class="chapter " data-level="1.5.2" >
<span>
FrequencyObservables
</span>
<ul class="articles">
<li class="chapter " data-level="1.5.2.1" data-path="../api/modules/_frequency_accounts_.html">
<a href="../api/modules/_frequency_accounts_.html">
frequency/accounts
</a>
</li>
<li class="chapter " data-level="1.5.2.2" data-path="../api/modules/_frequency_blocks_.html">
<a href="../api/modules/_frequency_blocks_.html">
frequency/blocks
</a>
</li>
<li class="chapter " data-level="1.5.2.3" data-path="../api/modules/_frequency_frequency_.html">
<a href="../api/modules/_frequency_frequency_.html">
frequency/frequency
</a>
</li>
<li class="chapter " data-level="1.5.2.4" data-path="../api/modules/_frequency_health_.html">
<a href="../api/modules/_frequency_health_.html">
frequency/health
</a>
</li>
<li class="chapter " data-level="1.5.2.5" data-path="../api/modules/_frequency_other_.html">
<a href="../api/modules/_frequency_other_.html">
frequency/other
</a>
</li>
<li class="chapter " data-level="1.5.2.6" data-path="../api/modules/_frequency_time_.html">
<a href="../api/modules/_frequency_time_.html">
frequency/time
</a>
</li>
<li class="chapter " data-level="1.5.2.7" data-path="../api/modules/_frequency_utils_createpubsubobservable_.html">
<a href="../api/modules/_frequency_utils_createpubsubobservable_.html">
frequency/utils/createPubsubObservable
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.3" >
<span>
RpcObservables
</span>
<ul class="articles">
<li class="chapter " data-level="1.5.3.1" data-path="../api/modules/_rpc_eth_.html">
<a href="../api/modules/_rpc_eth_.html">
rpc/eth
</a>
</li>
<li class="chapter " data-level="1.5.3.2" data-path="../api/modules/_rpc_net_.html">
<a href="../api/modules/_rpc_net_.html">
rpc/net
</a>
</li>
<li class="chapter " data-level="1.5.3.3" data-path="../api/modules/_rpc_other_makecontract_.html">
<a href="../api/modules/_rpc_other_makecontract_.html">
rpc/other/makeContract
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.3.3.1" data-path="../api/interfaces/_rpc_other_makecontract_.makecontract.html">
<a href="../api/interfaces/_rpc_other_makecontract_.makecontract.html">
MakeContract
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.3.4" data-path="../api/modules/_rpc_other_post_.html">
<a href="../api/modules/_rpc_other_post_.html">
rpc/other/post
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.3.4.1" data-path="../api/interfaces/_rpc_other_post_.postoptions.html">
<a href="../api/interfaces/_rpc_other_post_.postoptions.html">
PostOptions
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.3.5" data-path="../api/modules/_rpc_parity_.html">
<a href="../api/modules/_rpc_parity_.html">
rpc/parity
</a>
</li>
<li class="chapter " data-level="1.5.3.6" data-path="../api/modules/_rpc_rpc_.html">
<a href="../api/modules/_rpc_rpc_.html">
rpc/rpc
</a>
</li>
<li class="chapter " data-level="1.5.3.7" data-path="../api/modules/_rpc_utils_createrpc_.html">
<a href="../api/modules/_rpc_utils_createrpc_.html">
rpc/utils/createRpc
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.4" data-path="../api/modules/_types_.html">
<a href="../api/modules/_types_.html">
Types
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.4.1" data-path="../api/interfaces/_types_.frequencyobservable.html">
<a href="../api/interfaces/_types_.frequencyobservable.html">
FrequencyObservable
</a>
</li>
<li class="chapter " data-level="1.5.4.2" data-path="../api/interfaces/_types_.frequencyobservableoptions.html">
<a href="../api/interfaces/_types_.frequencyobservableoptions.html">
FrequencyObservableOptions
</a>
</li>
<li class="chapter " data-level="1.5.4.3" data-path="../api/interfaces/_types_.makecontract.html">
<a href="../api/interfaces/_types_.makecontract.html">
MakeContract
</a>
</li>
<li class="chapter " data-level="1.5.4.4" data-path="../api/interfaces/_types_.metadata.html">
<a href="../api/interfaces/_types_.metadata.html">
Metadata
</a>
</li>
<li class="chapter " data-level="1.5.4.5" data-path="../api/interfaces/_types_.rpcobservable.html">
<a href="../api/interfaces/_types_.rpcobservable.html">
RpcObservable
</a>
</li>
<li class="chapter " data-level="1.5.4.6" data-path="../api/interfaces/_types_.rpcobservableoptions.html">
<a href="../api/interfaces/_types_.rpcobservableoptions.html">
RpcObservableOptions
</a>
</li>
<li class="chapter " data-level="1.5.4.7" data-path="../api/interfaces/_types_.txstatus.html">
<a href="../api/interfaces/_types_.txstatus.html">
TxStatus
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.5" >
<span>
Utils
</span>
<ul class="articles">
<li class="chapter " data-level="1.5.5.1" data-path="../api/modules/_utils_isloading_.md">
<span>
utils/isLoading
</a>
</li>
<li class="chapter " data-level="1.5.5.2" data-path="../api/modules/_utils_isobservable_.html">
<a href="../api/modules/_utils_isobservable_.html">
utils/isObservable
</a>
</li>
<li class="chapter " data-level="1.5.5.3" data-path="../api/modules/_utils_operators_distinctreplayrefcount_.md">
<span>
utils/operators/distinctReplayRefCount
</a>
</li>
<li class="chapter " data-level="1.5.5.4" data-path="../api/modules/_utils_operators_distinctvalues_.html">
<a href="../api/modules/_utils_operators_distinctvalues_.html">
utils/operators/distinctValues
</a>
</li>
<li class="chapter " data-level="1.5.5.5" data-path="../api/modules/_utils_operators_switchmappromise_.html">
<a href="../api/modules/_utils_operators_switchmappromise_.html">
utils/operators/switchMapPromise
</a>
</li>
<li class="chapter " data-level="1.5.5.6" data-path="../api/modules/_utils_testhelpers_mockapi_.html">
<a href="../api/modules/_utils_testhelpers_mockapi_.html">
utils/testHelpers/mockApi
</a>
</li>
<li class="chapter " data-level="1.5.5.7" data-path="../api/modules/_utils_testhelpers_mockrpc_.html">
<a href="../api/modules/_utils_testhelpers_mockrpc_.html">
utils/testHelpers/mockRpc
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >Installation</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="installation">Installation</h1>
<h2 id="from-npm">From npm</h2>
<pre><code class="lang-bash">yarn add rxjs @parity/light.js <span class="hljs-comment"># Or npm install</span>
</code></pre>
<p>RxJS is a needed peer-dependency.</p>
<h2 id="usage">Usage</h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> light, { balanceOf$ } <span class="hljs-keyword">from</span> <span class="hljs-string">'./light.js'</span>;
light.setProvider(<span class="hljs-comment">/* put your Ethereum provider here */</span>);
<span class="hljs-comment">// Will print every time balance changes.</span>
balanceOf$(<span class="hljs-string">'0xbb9bc244d798123fde783fcc1c72d3bb8c189413'</span>).subscribe(<span class="hljs-built_in">console</span>.log);
</code></pre>
<p>Be sure to define your provider before using any of the other functions in <code>@parity/light.js</code>, or else the library will throw an error!</p>
<p>Here are some examples of providers you can use.</p>
<h3 id="provider-1-your-own-light-client-recommended-way-for-development">Provider 1: Your own Light Client (Recommended Way for development)</h3>
<p>Make sure a Light Client is running locally with a WebSocket server on port 8546. Follow <a href="../guides/tutorial1-set-up-a-light-client.html">this tutorial</a> to learn how to do it.</p>
<p>Then create a new provider that connects to the Light Client:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> Api <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/api'</span>;
<span class="hljs-keyword">import</span> light <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/light.js'</span>;
<span class="hljs-comment">// The dapp and the Light Client communicate via WebSocket. Other</span>
<span class="hljs-comment">// possibilities are Http or IPC.</span>
<span class="hljs-keyword">const</span> provider = <span class="hljs-keyword">new</span> Api.Provider.Ws(<span class="hljs-string">'ws://127.0.0.1:8546'</span>);
light.setProvider(provider);
<span class="hljs-comment">// Your app...</span>
</code></pre>
<p>Note: <code>@parity/api</code> is Parity's version of <a href="https://github.com/ethereum/web3.js/" target="_blank">web3.js</a>, but you can also web3.js instead. Please refer to <a href="https://github.com/paritytech/js-libs/tree/master/packages/api" target="_blank"><code>@parity/api</code> documentation</a> to see the difference.</p>
<p>Note: As of August 2018, this method is only suitable for local development only. If you put your dapp online, there is no easy way it'll have its own "local" Light Client. We recommend using another provider in this case.</p>
<p>However, if you are building a mobile application or an Electron desktop application, then the Light Client can be easily embedded into the application itself. For an example, see <a href="https://github.com/paritytech/fether" target="_blank">Fether</a>.</p>
<h3 id="provider-2-metamask">Provider 2: MetaMask</h3>
<p>Make sure you have <a href="https://metamask.io/" target="_blank">MetaMask</a> installed. Then,</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> light <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/light.js'</span>;
<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'load'</span>, () => {
<span class="hljs-comment">// Wait for web3.currentProvider to be injected</span>
light.setProvider(<span class="hljs-built_in">window</span>.web3.currentProvider);
startApp();
});
</code></pre>
<h3 id="provider-3-infura">Provider 3: INFURA</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> Api <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/api'</span>;
<span class="hljs-keyword">import</span> light <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/light.js'</span>;
<span class="hljs-comment">// The dapp and the INFURA node communicate via WebSockets.</span>
<span class="hljs-keyword">const</span> provider = <span class="hljs-keyword">new</span> Api.Provider.Ws(<span class="hljs-string">'wss://mainnet.infura.io/ws'</span>);
light.setProvider(provider);
<span class="hljs-comment">// Your app...</span>
</code></pre>
<h3 id="provider-4-your-own-full-node">Provider 4: Your own Full Node</h3>
<p>Make sure you have a full node running.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> Api <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/api'</span>;
<span class="hljs-keyword">import</span> light <span class="hljs-keyword">from</span> <span class="hljs-string">'@parity/light.js'</span>;
<span class="hljs-comment">// The dapp and the full node communicate via WebSocket. Other</span>
<span class="hljs-comment">// possibilities are Http or IPC.</span>
<span class="hljs-keyword">const</span> provider = <span class="hljs-keyword">new</span> Api.Provider.Ws(<span class="hljs-string">'ws://127.0.0.1:8546'</span>);
light.setProvider(provider);
<span class="hljs-comment">// Your app...</span>
</code></pre>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="does-it-work-with-a-full-node.html" class="navigation navigation-next navigation-unique" aria-label="Next page: Does this work with a full node?">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Installation","level":"1.2.1","depth":2,"next":{"title":"Does this work with a full node?","level":"1.2.2","depth":2,"path":"getting-started/does-it-work-with-a-full-node.md","ref":"getting-started/does-it-work-with-a-full-node.md","articles":[]},"previous":{"title":"Getting started","level":"1.2","depth":1,"ref":"","articles":[{"title":"Installation","level":"1.2.1","depth":2,"path":"getting-started/installation.md","ref":"getting-started/installation.md","articles":[]},{"title":"Does this work with a full node?","level":"1.2.2","depth":2,"path":"getting-started/does-it-work-with-a-full-node.md","ref":"getting-started/does-it-work-with-a-full-node.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"getting-started/installation.md","mtime":"2020-02-10T09:51:45.380Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-02-10T09:56:25.139Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>