Discovery Swarm WebRTC hooks for react.
$ npm install @geut/swarm-hooks @geut/discovery-swarm-webrtc
or
$ yarn add @geut/swarm-hooks @geut/discovery-swarm-webrtc
// ./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
Keeps reference to multiple swarms based on his config.
ReactElement
| required
React children.
Creates and provides an instance of discoverySwarmWebrtc
.
string
| defaults to 'default'
Identifies your swarm for access it later with useSwarm
.
object
Object containing options as defined in swarm creation
ReactElement
| required
React children.
Hook to get the swarm instance defined by id
object
| required
string
| defaults to 'default'
Identifies a <Swarm />
previously created. default
will be selected if no present.
discoverySwarmWebrtc
instance.
array
Array of connected peers. See getPeers
(conn, info) => HypercoreProtocol
Hook to join into a particular topic
object
| required
Buffer
| required
Topic to join.
string
| defaults to 'default'
Identifies a <Swarm />
previously created. default
will be selected if no present.
DiscoverySwarmWebrtc
discoverySwarmWebrtc
instance.
array
Array of connected peers. See getPeers
(conn, info) => HypercoreProtocol
🐛 If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.
👥 Ideas and contributions to the project are welcome. You must follow this guideline.
MIT © A GEUT project