react-socket-event
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Socket Event for React

npm-version


An easy way to create and call events within your React application.


Installation

yarn add react-socket-event

Quick Start

import SocketEvent from 'react-socket-event';

useEffect(() => {
    const removeListener = SocketEvent.on('my-event', (data) => {
        console.log(data);
    });

    return removeListener; //remove listener on unmount the component
}, []);

SocketEvent.emit('my-event', 'hello, world');
SocketEvent.clear('my-event'); //remove all listeners to 'my-event'

SocketEvent.clearAll(); //remove all listeners

SocketEvent.getAllListeners(); //return all listeners [{id, chanel, cb, key}]

Overwrite repeated listeners

import SocketEvent from 'react-socket-event';

SocketEvent.on(
    'my-event',
    (data) => {
        console.log(data);
    },
    'MY_UNIQUE_LISTENER'
);

// overwrites the previous listener avoiding multiple listeners
SocketEvent.on(
    'my-event',
    (data) => {
        console.log(data);
    },
    'MY_UNIQUE_LISTENER'
);

// will not overwrite
SocketEvent.on(
    'my-event',
    (data) => {
        console.log(data);
    },
    'MY_OTHER_LISTENER'
);

SocketEvent.emit('my-event', 'hello, world');

Package Sidebar

Install

npm i react-socket-event

Weekly Downloads

86

Version

2.0.0

License

MIT

Unpacked Size

10.1 kB

Total Files

10

Last publish

Collaborators

  • joseviniciusnunes