Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
react-query-subscription
Advanced tools
Changelog
Readme
useSubscription
hookWhile React Query is very feature rich, it misses one thing - support for streams, event emitters, WebSockets etc. This library leverages React Query's useQuery
to provide useSubscription
hook for subscribing to real-time data.
React Query useQuery
's query function is any function which returns a Promise. Similarly, useSubscription
's subscription function is any function which returns an Observable.
npm install react-query-subscription react react-query@3 rxjs@7
or
yarn add react-query-subscription react react-query@3 rxjs@7
Please see examples.
TODO
import { QueryClientProvider, QueryClient } from 'react-query';
import { ReactQueryDevtools } from 'react-query/devtools';
import { useSubscription } from 'react-query-subscription';
import { eventSource$ } from 'rx-event-source';
const queryClient = new QueryClient();
function App() {
return (
<QueryClientProvider client={queryClient}>
<SseExample />
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}
function SseExample() {
const { data, isLoading, isError, error } = useSubscription(
'some-key',
// @see https://kaciakmaciak.github.io/rx-event-source/modules.html#eventSource_
() => eventSource$('/api/v1/sse'),
{
// options
}
);
if (isLoading) {
return <div>Loading...</div>;
}
if (isError) {
return (
<div role="alert">
{error?.message || 'Unknown error'}
</div>
);
}
return <div>Data: {JSON.stringify(data)}</div>;
}
See rx-event-source
docs.
graphql-ws
import { QueryClientProvider, QueryClient } from 'react-query';
import { ReactQueryDevtools } from 'react-query/devtools';
import { useSubscription } from 'react-query-subscription';
import { Observable } from 'rxjs';
import { createClient } from 'graphql-ws';
import type { Client, SubscribePayload } from 'graphql-ws';
const queryClient = new QueryClient();
function App() {
return (
<QueryClientProvider client={queryClient}>
<GraphQlWsExample postId="abc123" />
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}
const client = createClient({ url: 'wss://example.com/graphql' });
/**
* @see https://github.com/enisdenjo/graphql-ws#observable
*/
export function fromWsClientSubscription<TData = Record<string, unknown>>(
client: Client,
payload: SubscribePayload
) {
return new Observable<TData | null>((observer) =>
client.subscribe<TData>(payload, {
next: (data) => observer.next(data.data),
error: (err) => observer.error(err),
complete: () => observer.complete(),
})
);
}
interface Props {
postId: string;
}
interface Comment {
id: string;
content: string;
}
function GraphQlWsExample({ postId }: Props) {
const { data, isLoading, isError, error } = useSubscription(
'some-key',
() => fromWsClientSubscription<{ comments: Array<Comment> }>({
query: `
subscription Comments($postId: ID!) {
comments(postId: $postId) {
id
content
}
}
`,
variables: {
postId,
},
}),
{
// options
}
);
if (isLoading) {
return <div>Loading...</div>;
}
if (isError) {
return (
<div role="alert">
{error?.message || 'Unknown error'}
</div>
);
}
return <div>Data: {JSON.stringify(data?.comments)}</div>;
}
Thanks goes to these wonderful people (emoji key):
Katarina Anton 💻 🤔 🚧 ⚠️ 🔧 🚇 | Jacob Cable 💻 🤔 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
Hook for managing, caching and syncing observables in React
The npm package react-query-subscription receives a total of 138 weekly downloads. As such, react-query-subscription popularity was classified as not popular.
We found that react-query-subscription demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.