
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
streamr-client-react
Advanced tools
React hooks and components for streamr-client
.
Using npm
, install the library, and save it to your package.json
dependencies.
npm i streamr-client-react react-fast-compare streamr-client streamr-client-protocol
Provider
It holds its own StreamrClient
instance and – by utilizing the Context API – makes it available to all its children components.
import Provider from 'streamr-client-react'
function App() {
return <Provider {...options}>You can use `useClient` here!</Provider>
}
ClientContext
If you wanna hack your way around the useClient
hook for some wholesome reason and directly access the client instance provided by the Provider
component this is where you start.
import { useContext } from 'react'
import type { StreamrClient } from 'streamr-client'
function SqrtOfFoo() {
const client: undefined | StreamrClient = useContext(ClientContext)
return null
}
useClient(config?: StreamrClientConfig)
If config
is given, useClient
gives you a new instance of the client. The hook uses react-fast-compare
to avoid unreasonable creation of new instances.
If config
is skipped, it's gonna return an instance provided by the Provider
component (undefined
by default).
See Config.ts for more details on StreamrClientConfig
.
useSubscribe(streamId: string, options?: Options)
It allows you to conveniently subscribe to streams.
import type { ResendOptions, StreamMessage } from 'streamr-client'
interface Options {
// Changing `cacheKey` will drop the old subscription and start a new one.
cacheKey?: number | string
// Set `disabled` to true to make it idle, or to make it drop the previous subscription
// and then idle.
disabled?: boolean
// You can either skip undecoded messages (true), or treat them as other messages (false), and
// handle their undecoded content on your own. Useful when you have to show "something".
// Default: false
ignoreUndecodedMessages?: boolean
// A callback triggered after you're done with a subscription and with processing messages.
onAfterFinish?: () => void
// A callback triggered before subscribing.
onBeforeStart?: () => void
// A callback triggered when the client fails at subscribing.
onError?: (e: any) => void
// *The* on-message callback.
onMessage?: (msg: StreamMessage) => void
// A callback triggered when the client fails to decode a massage.
onMessageError?: (e: any) => void
// Resend instructions. `undefined` by default (= don't resend anything).
resendOptions?: ResendOptions
}
onAfterFinish
, onBeforeStart
, onError
, onMessage
, and onMessageError
are all kept as refs (see useRef
) internally, and for that reason changing them does not trigger resubscribing. Additionally, we track changes to resendOptions
using react-fast-compare
to avoid excessive resubscribing.
See
client/src/subscribe/Resends.ts
for more details on ResendOptions
.useResend(streamId: string, resendOptions: ResendOptions, options?: Options)
It allows you to resend historical messages without subscribing to the real-time messages.
import type { ResendOptions, Message } from 'streamr-client'
interface Options {
// Changing `cacheKey` will drop the old subscription and start a new one.
cacheKey?: number | string
// Set `disabled` to true to make it idle, or to make it drop the previous subscription
// and then idle.
disabled?: boolean
// You can either skip undecoded messages (true), or treat them as other messages (false), and
// handle their undecoded content on your own. Useful when you have to show "something".
// Default is false.
ignoreUndecodedMessages?: boolean
// A callback triggered after you're done with a subscription and with processing messages.
onAfterFinish?: () => void
// A callback triggered before subscribing.
onBeforeStart?: () => void
// A callback triggered when the client fails at subscribing.
onError?: (e: any) => void
// *The* on-message callback.
onMessage?: (msg: Message) => void
// A callback triggered when the client fails to decode a massage.
onMessageError?: (e: any) => void
}
See
client/src/subscribe/Resends.ts
for more details on ResendOptions
.client/src/Message.ts
for more details on Message
.subscribe(streamId: string, client: StreamrClient, options?: Options)
Subscribes to a stream and returns an object with 2 asynchrounous methods: next
and abort
. Example:
async function foo(streamId: string, client: StreamrClient) {
const queue = subscribe(streamId, client)
while (true) {
const { value: msg, done } = await queue.next()
if (msg) {
// Do something with a message here.
}
if (done) {
break
}
}
// Use `queue.abort()` to unsubscribe.
}
Available options:
interface Options {
// You can either skip undecoded messages (true), or treat them as other messages (false), and
// handle their undecoded content on your own. Useful when you have to show "something".
// Default is false.
ignoreUndecodedMessages?: boolean
// A callback triggered when the client fails at subscribing.
onError?: (e: any) => void
// Resend instructions. `undefined` by default (= don't resend anything).
onMessageError?: (e: any) => void
}
resend(streamId: string, resendOptions: ResendOptions, streamrClient: StreamrClient, options?: Options)
Subscribes to a stream of historical messages (only) and returns an object with 2 asynchrounous methods: next
and abort
. Example:
async function foo(streamId: string, client: StreamrClient) {
const queue = resend(streamId, { last: 10 }, client)
while (true) {
const { value: msg, done } = await queue.next()
if (msg) {
// Do something with a message here.
}
if (done) {
break
}
}
// Use `queue.abort()` to ignore further data.
}
subscribe
and resend
share the options.
FAQs
React toolkit for Streamr.
We found that streamr-client-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.