Git Product home page Git Product logo

react-signalr's Introduction

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(
    "event name", // Your Event Key
    (message) => {
      setMessage([...messages, message]);
    },
  );

  return <Components />;
};

React-Native

Full supported

react-signalr's People

Contributors

hosseinmd avatar heydarideveloper avatar kkfuri avatar majidbigdeli avatar tberg-bcgdv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.