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.11
Source
npmnpm
Version published
Weekly downloads
20
-76.47%
Maintainers
1
Weekly downloads
 
Created
Source

NPM Version

useSignalRHub

An easy to use React hook for @microsoft/signalr

Please use Microsoft's Documentation and API Reference as guidance.

Getting started

Install using your preferred package manager

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

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
})

Api

const signalRHub = useSignalRHub(hubUrl, {
  onConnected,
  onDisconnected,
  onReconnecting,
  onReconnected,
  onError,
  enabled,
  automaticReconnect,
  httpTransportTypeOrOptions,
  hubProtocol,
  logging
})

Options

onConnected?: (hub: HubConnection) => void
onDisconnected?: (error?: Error) => void
onReconnecting?: (error?: Error) => void
onReconnected?: (connectionId?: string) => void
onError?: (error?: Error) => void
enabled?: boolean
automaticReconnect?: number[] | IRetryPolicy | boolean
httpTransportTypeOrOptions?: IHttpConnectionOptions | HttpTransportType
hubProtocol?: IHubProtocol
logging?: LogLevel | string | ILogger

HubConnection | IRetryPolicy | IHttpConnectionOptions | HttpTransportType | IHubProtocol | LogLevel | ILogger

Returns

signalRHub: HubConnection | null

HubConnection

Keywords

react

FAQs

Package last updated on 14 Jun 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