npm

@enymo/react-socket-hook
TypeScript icon, indicating that this package has built-in type declarations

1.1.4 • Public • Published

React Socket Hook

A simple hook providing a declarative syntax for listening to websocket events using socket.io

This library was mainly created for use with the React Resource Hook, but can be used by itself nevertheless.

Installation

The library may be installed from npm using

npm i @enymo/react-socket-hook

Usage

In order to use the useSocket hook in your components, you app has to be wrapped with the SocketProvider to configure the hook

import { SocketProvider } from "@enymo/react-socket-hook";
import React from "react";
import { io } from "socket.io-client";

function App() {
    const client = io("https://www.example.com");

    return (
        <SocketProvider value={client}>
            {/* your app here */}
        </SocketProvider>
    )
}

You can then listen to socket events in your components like this:

import React from "react";
import useSocket from "@enymo/react-socket-hook";

export default function Component() {
    useSocket("event", (payload: any) => {
        // do something with payload
    }, []);

    return null;
}

Readme

Keywords

none

Package Sidebar

Install

npm i @enymo/react-socket-hook

Weekly Downloads

1

Version

1.1.4

License

MIT

Unpacked Size

7.66 kB

Total Files

7

Last publish

Collaborators

  • sszajbely
  • jserguhn
  • gritter