React Hook for SSE
npm install react-hooks-sse 
```
yarn add react-hooks-sse
`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 on the project
#### 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: trueAuthorization
- provide a custom source to control the network request e.g. set header
Here 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 = () => (
{/ ... /}
);
`
----
#### useSSE(eventName: string, initialState: S, options?: Options)
The component that uses the hook must be scoped under a 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
type StateReducer = (state: S, changes: Action
type Parser
export type Options = {
stateReducer?: StateReducer;
parser?: Parser
};
`
#### options.stateReducer?: (state: S, changes: Action
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?:
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);
},
}
);
``
yarn start:server
``
yarn start:example
``
yarn build
```
yarn test
[EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/EventSource