All Projects → samouss → react-hooks-sse

samouss / react-hooks-sse

Licence: MIT license
Subscribe to an SSE endpoint with React Hooks

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects
shell
77523 projects

Projects that are alternatives of or similar to react-hooks-sse

geo-smart-system
Open Source Realtime Tracking System
Stars: ✭ 36 (-40%)
Mutual labels:  sse
useReduction
useReducer without boilerplate
Stars: ✭ 36 (-40%)
Mutual labels:  react-hooks
hookstore
Hook based and lightweight centralized state management for React.
Stars: ✭ 28 (-53.33%)
Mutual labels:  react-hooks
book-fullstack-react
Fullstack React: The Complete Guide to ReactJS and Friends by Anthony Accomazzo
Stars: ✭ 100 (+66.67%)
Mutual labels:  react-hooks
7-react-admin-ts
用 ts + react-hooks 实现的管理后台
Stars: ✭ 23 (-61.67%)
Mutual labels:  react-hooks
react-click-away-listener
🐾 Tiny React Click Away Listener built with React Hooks
Stars: ✭ 131 (+118.33%)
Mutual labels:  react-hooks
FFmpegPlayer
Simple FFmpeg video player
Stars: ✭ 72 (+20%)
Mutual labels:  sse
use-breakpoint
React `useBreakpoint` hook to have different values for a variable based on a breakpoints.
Stars: ✭ 17 (-71.67%)
Mutual labels:  react-hooks
sseserver
🏄 High-performance Server-Sent Events endpoint for Go
Stars: ✭ 88 (+46.67%)
Mutual labels:  sse
babel-plugin-file-loader
Like webpack's file-loader, but on server side. Allows for production-grade require('./file.png')
Stars: ✭ 36 (-40%)
Mutual labels:  sse
react-hooks-gatsby
Learn how to use React Hooks, and how they work with Gatsby. Watch the livestream:
Stars: ✭ 18 (-70%)
Mutual labels:  react-hooks
use-socket.io-client
https://www.npmjs.com/package/use-socket.io-client
Stars: ✭ 47 (-21.67%)
Mutual labels:  react-hooks
use-monaco
Use 🗒️ monaco-editor in any ⚛️ React app with simple hooks 🎣
Stars: ✭ 85 (+41.67%)
Mutual labels:  react-hooks
use-local-storage
A flexible React Hook for using Local Storage.
Stars: ✭ 57 (-5%)
Mutual labels:  react-hooks
react-native-react-bridge
An easy way to integrate your React (or Preact) app into React Native app with WebView.
Stars: ✭ 84 (+40%)
Mutual labels:  react-hooks
acharep
🏠 App to find fraternities near your university
Stars: ✭ 23 (-61.67%)
Mutual labels:  react-hooks
use-detect-print
A react hook to detect when a page is being printed
Stars: ✭ 55 (-8.33%)
Mutual labels:  react-hooks
furl
Functional react.js components.
Stars: ✭ 33 (-45%)
Mutual labels:  react-hooks
react-hubspot
A collection of React hooks for interacting with Hubspot APIs
Stars: ✭ 20 (-66.67%)
Mutual labels:  react-hooks
pokehooks-labs
A laboratory to use pokemons and do some experiments with React Hooks API
Stars: ✭ 35 (-41.67%)
Mutual labels:  react-hooks

React Hooks SSE

npm version Build Status

Installation

yarn add react-hooks-sse

Usage

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 = () => (
  <SSEProvider endpoint="https://sse.example.com">
    <h1>Subscribe & update to SSE event</h1>
    <Comments />
  </SSEProvider>
);

Checkout the 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

import React from 'react';
import { SSEProvider } from 'react-hooks-sse';

const App = () => (
  <SSEProvider endpoint="https://sse.example.com">
    {/* ... */}
  </SSEProvider>
);

endpoint: string

The value is required when source is omitted.

The SSE endpoint to target. It uses the default source EventSource.

import React from 'react';
import { SSEProvider } from 'react-hooks-sse';

const App = () => (
  <SSEProvider endpoint="https://sse.example.com">
    {/* ... */}
  </SSEProvider>
);

source: () => Source

The value is required when endpoint is omitted.

You can provide custom source to the provider. The main use cases are:

Here is the interface that a source has to implement:

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.

import React from 'react';
import { SSEProvider } from 'react-hooks-sse';
import { createCustomSource } from 'custom-source';

const App = () => (
  <SSEProvider source={() => createCustomSource()}>
    {/* ... */}
  </SSEProvider>
);

useSSE<S, T>(eventName: string, initialState: S, options?: Options<S, T>)

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

const state = useSSE('comments', {
  count: null
});

eventName: string

The name of the event that you want to listen.

const state = useSSE('comments', {
  count: null
});

initialState: S

The initial state to use on the first render.

const state = useSSE('comments', {
  count: null
});

options?: Options<S, T>

The options to control how the data is consumed from the source.

type Action<T> = { event: Event; data: T };
type StateReducer<S, T> = (state: S, changes: Action<T>) => S;
type Parser<T> = (data: any) => T;

export type Options<S, T = S> = {
  stateReducer?: StateReducer<S, T>;
  parser?: Parser<T>;
};

options.stateReducer?: <S, T>(state: S, changes: Action<T>) => S

The reducer to control how the state should be updated.

type Action<T> = {
  // event is provided by the source
  event: Event;
  // data is provided by the parser
  data: T;
};

const state = useSSE<S, T>(
  'comments',
  {
    count: null,
  },
  {
    stateReducer(state: S, action: Action<T>) {
      return changes.data;
    },
  }
);

options.parser?: <T>(data: any) => T

The parser to control how the event from the server is provided to the reducer.

const state = useSSE<S, T>(
  '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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].