1.0.0 • Published 4 years ago

@ahmadster/usews v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

useWS - React Hooks Websocket

The easiest and most fuss-free Websocket implementation for React Hooks.

Quick Start

Add useWS to your project, npm or yarn:

yarn add useWS

Simplist example using the defaults

import React, {useReducer} from "react"
import {useWS} from "./useWS"

export default function App() {
  // our simple message handler

  const handleMessage = (msg) => console.log(`Got message`,msg)
  // connect to the /ws endpoint on the same url by default passing our handler
  const [ws, wsStatus] = useWS(handleMessage)

  // send a message to the server
  const sendData = () => {
   	ws.sendJson({"re": `hello world`})
  }

  return <div className={`App`}>
    The Websocket is {wsStatus}
    <button onClick={()=>sendData()}>Say Hello</div>
  </div>
}

Customizing

import {useWS} from "./useWS"

// Optional overrides, defaults shown here
let wsOptions = {
  wsURL: () => `${window.location.origin.replace(`http`, `ws`)}/ws`,
  maxMissedHeartbeats: 3,
  reconnectDelay: 20000,
  pingDelay: 500,
  pingPayload: {re: `ping`},
  pongPayload: {re: `pong`},
  debugMode: false,
  onOpen: undefined,
  onClose: undefined,
  onMessage: undefined,
  onError: undefined
}
//
//
//
const [ws, wsStatus] = useWS(handleMessage, wsOptions)