You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

use-signalr-hub

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-signalr-hub

Easy to use hook for signalr

0.0.9
Source
npmnpm
Version published
Weekly downloads
67
139.29%
Maintainers
1
Weekly downloads
 
Created
Source

useSignalRHub

Experimental

Easy to use React hook for signalR

Getting started

Install using your preferred package manager:

npm install use-signalr-hub
yarn add use-signalr-hub

How to use

Import into your project:

import signalR, { useSignalRHub } from "use-signalr-hub"

Use in your React component

const signalRHub = useSignalRHub("/chathub", {
  onConnected: (hub) => {
    console.log("Connected to /chathub", hub)
    hub.on("ReceiveMessage", (user, message) => {
      setMessage({user, message})
    })
  },
  onDisconnected: () => {
    console.log("Disconnected from /chathub")
  },
  onError: (error) => {
    console.log("Connection to /chathub failed with error", error)
  }
})

const handleSubmit = (message, user) => {
  signalRHub.invoke("SendMessage", user, message)
    .catch((error) => {
      console.log("Failed to invoke SendMessage on /chathub", error)
    })
}

configure defaults

signalR.setDefaults({
  httpTransportTypeOrOptions: {
    accessTokenFactory: () => user.userData.token
  },
  automaticReconnect: false
})

Upcomming features

  • useSharedSignalRHub

Keywords

react

FAQs

Package last updated on 26 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts