Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-signalr

hosseinmd20.3kMIT0.2.24TypeScript support: included

React-signalr is a tools for using signalr web socket in react/react-native apps

react-signalr, react, signalr, web socket, socket, microsoft

readme

React-Signalr

NPM

install size dependencies

React-Signalr Is a tools for using signalR, Socket.io or WebSocket in react/react-native apps

  • Supported microsoft/signalR version 5 and later
  • Supported Socket.io
  • Supported WebSocket

Features

  • Hooks for connect event to a component
  • Manage connections in multiple tabs (SignalR can only have about 6 tabs open). React-signalr will create a connection open and send event to other tabs by hermes-channel
  • Handle reconnect

TOC

install

$ yarn add react-signalr @microsoft/signalr socket.io-client

Get started

signalr

First of all you need to create a signalR context. every thing is depend on your context, you could create multiple context.

import { createSignalRContext } from "react-signalr/signalr";

const SignalRContext = createSignalRContext();

const App = () => {
  const { token } = YourAccessToken;

  return (
    <SignalRContext.Provider
      connectEnabled={!!token}
      accessTokenFactory={() => token}
      dependencies={[token]} //remove previous connection and create a new connection if changed
      url={"https://example/hub"}
    >
      <Routes />
    </SignalRContext.Provider>
  );
};

useSignalREffect

Use this for connect to an event

const Comp = () => {
  const [messages, setMessage] = useState([]);

  SignalRContext.useSignalREffect(
    "event name", // Your Event Key
    (message) => {
      setMessage([...messages, message]);
    },
  );

  return <Components />;
};

socketio

create a socketIO context,

import { createSocketIoContext } from "react-signalr/socketio";

const SocketIOContext = createSocketIoContext();

const App = () => {
  const { token } = YourAccessToken;

  return (
    <SocketIOContext.Provider
      connectEnabled={!!token}
      accessTokenFactory={() => token}
      dependencies={[token]} //remove previous connection and create a new connection if changed
      url={"https://example/hub"}
    >
      <Routes />
    </SocketIOContext.Provider>
  );
};

useSignalREffect

Use this to connect to an event

const Comp = () => {
  const [messages, setMessage] = useState([]);

  SocketIOContext.useSocketEffect(
    "event name", // Your Event Key
    (message) => {
      setMessage([...messages, message]);
    },
  );

  return <Components />;
};

websocket

create a websocket context,

import { createWebSocketContext } from "react-signalr/websocket";

const WebsocketContext = createWebSocketContext();

const App = () => {
  const { token } = YourAccessToken;

  return (
    <WebsocketContext.Provider
      connectEnabled={!!token}
      dependencies={[token]} //remove previous connection and create a new connection if changed
      url={"https://example/hub"}
    >
      <Routes />
    </WebsocketContext.Provider>
  );
};

useWebSocketEffect

Use this for connect to an event in you component

const Comp = () => {
  const [messages, setMessage] = useState([]);

  WebsocketContext.useWebSocketEffect(
    (message) => {
      setMessage([...messages, message]);
    },
  );

  return <Components />;
};

supports

react-signalr @microsoft/signalr
0.2.0 - 0.2.18 7.x
0.2.19 7.x - 8.x

React-Native

Full supported

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.2.24 (2024-09-28)

Bug Fixes

  • remove useless random-value package (e5f3803)

0.2.23 (2024-09-11)

Features

  • support react-native headers for websocket (9048654)

0.2.22 (2024-05-26)

Features

  • add retyrDelay to automaticReconnect (93b43b1)

0.2.21 (2024-05-26)

Bug Fixes

0.2.20 (2024-05-26)

Bug Fixes

0.2.19 (2024-01-29)

Bug Fixes

0.2.18 (2023-09-08)

Bug Fixes

0.2.17 (2023-06-25)

Features

0.2.16 (2023-05-20)

Bug Fixes

0.2.15 (2023-05-20)

Bug Fixes

0.2.13 (2022-07-23)

Bug Fixes

0.2.12 (2022-07-23)

Bug Fixes

0.2.11 (2022-04-19)

Features

  • add onReconnect event to signalr (763053f)

0.2.10 (2022-03-01)

Bug Fixes

0.2.9 (2022-03-01)

Features

  • add onOpen, onClosed and onBeforeClose events to signalr provider (67ad747)

0.2.8 (2022-02-20)

Bug Fixes

  • react-native providerFactory export (4b78c25)

0.2.7 (2022-01-30)

Bug Fixes

0.2.5 (2022-01-27)

Features

0.2.3 (2021-12-28)

Bug Fixes

0.2.2 (2021-12-22)

Bug Fixes

  • add uuid react-native-get-random-values (5cda491)

0.2.1 (2021-12-22)

Features

  • disable Connection Between tabs (create connections for all tabs) (#5) (265b167)

Bug Fixes

0.2.0 (2021-12-07)

Features

  • disable Connection Between tabs (create connections for all tabs) (#5) (265b167)

0.1.6 (2021-10-01)

Bug Fixes

0.1.2 (2021-09-30)

Features

Bug Fixes

  • few missed socket context (eb6989b)

0.1.1 (2021-07-03)

Bug Fixes

  • get unlimited arguments (5fa0168)

0.1.0 (2021-06-02)

Features

0.0.10 (2020-12-30)

Features

Bug Fixes

  • types of callbacks and invoke (a532848)

0.0.9 (2020-12-27)

Features

  • add onError to provider (9f5cf5f)

Bug Fixes

  • split react-native provider (34500f3)

0.0.7 (2020-12-16)

Bug Fixes

  • don't reconnect if another tab connected (9e341eb)

0.0.6 (2020-12-16)

0.0.5 (2020-12-14)

0.0.4 (2020-12-14)

0.0.3 (2020-12-14)

Bug Fixes

0.0.2 (2020-12-14)

Bug Fixes

0.0.1 (2020-12-14)