https://github.com/samouss/react-hooks-sse
Subscribe to an SSE endpoint with React Hooks
https://github.com/samouss/react-hooks-sse
react-hooks sse
Last synced: 3 months ago
JSON representation
Subscribe to an SSE endpoint with React Hooks
- Host: GitHub
- URL: https://github.com/samouss/react-hooks-sse
- Owner: samouss
- License: mit
- Created: 2018-12-16T11:36:48.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T08:30:47.000Z (9 months ago)
- Last Synced: 2024-09-16T17:49:04.855Z (9 months ago)
- Topics: react-hooks, sse
- Language: TypeScript
- Size: 6.34 MB
- Stars: 97
- Watchers: 4
- Forks: 5
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# React Hooks SSE
[](https://badge.fury.io/js/react-hooks-sse) [](https://travis-ci.org/samouss/react-hooks-sse)
## Installation
```
yarn add react-hooks-sse
```## Usage
```jsx
import React from 'react';
import { useSSE, SSEProvider } from 'react-hooks-sse';const Comments = () => {
const state = useSSE('comments', {
count: null
});return state.count ? state.count : '...';
};const App = () => (
Subscribe & update to SSE event
);
```> Checkout [the example](/example) on the project
## API
#### `SSEProvider`
The provider manages subscriptions to the SSE server. You can subscribe multiple times to the same event or on different events. The source is lazy, it is created only when one of the hooks is called. The source is destroyed when no more hooks are registered. It is automatically re-created when a new hook is added.
#### Usage
```jsx
import React from 'react';
import { SSEProvider } from 'react-hooks-sse';const App = () => (
{/* ... */}
);
```#### `endpoint: string`
> The value is required when `source` is omitted.
The SSE endpoint to target. It uses the default source [`EventSource`][EventSource].
```jsx
import React from 'react';
import { SSEProvider } from 'react-hooks-sse';const App = () => (
{/* ... */}
);
```#### `source: () => Source`
> The value is required when `endpoint` is omitted.
You can provide custom source to the provider. The main use cases are:
- provide additional options to [`EventSource`][EventSource] e.g. [`withCredentials: true`](https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource#Parameters)
- provide a custom source to control the network request e.g. set `Authorization` headerHere is the interface that a source has to implement:
```ts
interface Event {
data: any;
}interface Listener {
(event: Event): void;
}interface Source {
addEventListener(name: string, listener: Listener): void;
removeEventListener(name: string, listener: Listener): void;
close(): void;
}
```The source is lazy, it is created only when a hook is added. That's why we provide a function to create a source not a source directly.
```jsx
import React from 'react';
import { SSEProvider } from 'react-hooks-sse';
import { createCustomSource } from 'custom-source';const App = () => (
createCustomSource()}>
{/* ... */}
);
```----
#### `useSSE(eventName: string, initialState: S, options?: Options)`
The component that uses the hook must be scoped under a [`SSEProvider`](#SSEProvider) to have access to the source. Once the hook is created none of the options can be updated (at the moment). You have to unmout/remount the component to update the options.
#### Usage
```jsx
const state = useSSE('comments', {
count: null
});
```#### `eventName: string`
The name of the event that you want to listen.
```jsx
const state = useSSE('comments', {
count: null
});
```#### `initialState: S`
The initial state to use on the first render.
```jsx
const state = useSSE('comments', {
count: null
});
```#### `options?: Options`
The options to control how the data is consumed from the source.
```ts
type Action = { event: Event; data: T };
type StateReducer = (state: S, changes: Action) => S;
type Parser = (data: any) => T;export type Options = {
stateReducer?: StateReducer;
parser?: Parser;
};
```#### `options.stateReducer?: (state: S, changes: Action) => S`
The reducer to control how the state should be updated.
```ts
type Action = {
// event is provided by the source
event: Event;
// data is provided by the parser
data: T;
};const state = useSSE(
'comments',
{
count: null,
},
{
stateReducer(state: S, action: Action) {
return changes.data;
},
}
);
```#### `options.parser?: (data: any) => T`
The parser to control how the event from the server is provided to the reducer.
```jsx
const state = useSSE(
'comments',
{
count: null,
},
{
parser(input: any): T {
return JSON.parse(input);
},
}
);
```## Run example
```
yarn start:server
``````
yarn start:example
```## Run the build
```
yarn build
```## Run the test
```
yarn test
```[EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/EventSource