@geut/swarm-hooks
v1.0.0-beta.11
Published
Swarm hooks
Downloads
32
Keywords
Readme
@geut/swarm-hooks
Discovery Swarm WebRTC hooks for react.
Table of Contents
Install
$ npm install @geut/swarm-hooks @geut/discovery-swarm-webrtc
or
$ yarn add @geut/swarm-hooks @geut/discovery-swarm-webrtc
Usage
// ./App.js
import React from 'react'
import { SwarmProvider, Swarm } from '@geut/swarm-hooks'
import Peers from './components/Peers'
function App () {
return (
<SwarmProvider>
<Swarm id='cool-swarm' config={{ bootstrap: ['wss://geut-webrtc-signal-v3.herokuapp.com'] }}>
<Peers />
</Swarm>
</SwarmProvider>
)
}
export default App
// ./components/Peers.js
import React, { useEffect } from 'react'
import crypto from 'crypto'
import { useJoin } from '@geut/swarm-hooks'
const someTopic = crypto.createHash('sha256')
.update('some-topic')
.digest()
function Peers () {
const { peers, useSubscription } = useJoin({ id: 'cool-swarm', topic: someTopic })
useSubscription('connection', (connection, info) => {
console.log('New peer!', connection, info)
})
useSubscription('connection-closed', (connection, info) => {
console.log('Peer disconnected', connection, info)
})
return (
<div>
<h1>Peers</h1>
<ul>
{peers.map(peer => <li>{peer.id.toString('hex')}</li>)}
</ul>
</div>
)
}
export default Peers
Api
SwarmProvider
Keeps reference to multiple swarms based on his config.
children
ReactElement
| required
React children.
Swarm
Creates and provides an instance of discoverySwarmWebrtc
.
id
string
| defaults to 'default'
Identifies your swarm for access it later with useSwarm
.
config
object
Object containing options as defined in swarm creation
children
ReactElement
| required
React children.
useSwarm(options)
Hook to get the swarm instance defined by id
options
object
| required
options.id
string
| defaults to 'default'
Identifies a <Swarm />
previously created. default
will be selected if no present.
Returns an object
with:
swarm
discoverySwarmWebrtc
instance.
peers
array
Array of connected peers. See getPeers
useSubscription
useHypercoreProtocol
(conn, info) => HypercoreProtocol
useJoin(options)
Hook to join into a particular topic
options
object
| required
options.topic
Buffer
| required
Topic to join.
options.id
string
| defaults to 'default'
Identifies a <Swarm />
previously created. default
will be selected if no present.
Returns an object
with:
swarm
DiscoverySwarmWebrtc
discoverySwarmWebrtc
instance.
peers
array
Array of connected peers. See getPeers
useSubscription
useHypercoreProtocol
(conn, info) => HypercoreProtocol
Issues
:bug: If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.
Contributing
:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this guideline.
License
MIT © A GEUT project