npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@scripters/use-socket.io

v1.6.0

Published

React hook for socket.io

Downloads

4

Readme

@scripters/use-socket.io

travis npm downloads eslint react

Use socket.io library easily with React hooks

Installation

use-socket.io is available as an npm package.

// using npm
npm i @scripters/use-socket.io

// using yarn
yarn add @scripters/use-socket.io

Usage

API

Provider

import React from 'react';
import { Provider } from '@scripters/use-socketio';

const SOCKET_URL = 'http://localhost:4000';
const SOCKET_OPTIONS = {
    forceNew: true,
};

<Provider url={SOCKET_URL} options={SOCKET_OPTIONS}>
    <App />
</Provider>

Provider with namespaces

import React from 'react';
import { Provider } from '@scripters/use-socketio';

const SOCKET_URL = 'http://localhost:4000';
const SOCKET_OPTIONS = {
    forceNew: true,
};

<Provider url={SOCKET_URL} options={SOCKET_OPTIONS} namespaces={['test']}>
    <App />
</Provider>

useSocket

import React, { useEffect, useState } from 'react';
import { useSocket } from '@scripters/use-socket.io';

const ChatStatus = () => {
    const socket = useSocket();
    const [user, setUser] = useState(null);

    useEffect(() => {
        socket.on('user', (userData) => {
            setUser(userData);
        });
    }, [socket]);

    const getMessage = () => user ? `User: ${user.name}` : 'User unauthenticated';

    return (
        <p>
            { getMessage() }
        </p>
    )
};

export default ChatStatus;

useListener

import React, { useState } from 'react';
import { useListener } from '@scripters/use-socket.io';

const ChatStatus = () => {
    const [user, setUser] = useState(null);
    
    useListener('user', setUser);

    const getMessage = () => user ? `User: ${user.name}` : 'User unauthenticated';

    return (
        <p>
            { getMessage() }
        </p>
    )
};

export default ChatStatus;

useListener with pause

import React, { useState } from 'react';
import { useListener } from '@scripters/use-socket.io';

const ChatStatus = () => {
    const [currentMessage, setMessage] = useState('');
    
    const [subscribeMessages, unsubscribeMessages ] = useListener('messages', setMessage);

    setTimeout(() => {
        unsubscribeMessages();
    }, 2000);

    setTimeout(() => {
        subscribeMessages();
    }, 5000);

    return (
        <p>{ currentMessage }</p>
    )
};

export default ChatStatus;

useEmit

import React from 'react';
import { useEmit } from '@scripters/use-socket.io';

const ChatMessage = () => {
    const emit = useEmit();

    const handleMessage = (message) => {
        emit('message', message);
    };

    return (
        <div>
           <button onClick={() => handleMessage('Test message')}>Send message</button>
        </div>
    )
};

export default ChatMessage;

Hints

Sometimes you want to be sure that all listeners are attached to the socket before connection is established. It's helpful when you want to handle events which socket server sends right after client is connected (e.g. connect). To solve that you can use socket option autoConnect: false and socket.open() right after all listeners attach call.

import React, { useState } from 'react';
import { Provider, useSocket, useListener } from '@scripters/use-socket.io';

const App = () => {
    const [connected, setConnected] = useState(false);
    const socket = useSocket();

    useListener('connect', () => setConnected(true));

    socket.open();

    return (connected ? 'User connected' : 'User disconnected');
}

<Provider url="http://localhost:4000/" options={{ autoConnect: false }}>
    <App />
</Provider>

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Roadmap

See the open issues for a list of proposed features (and known issues).

License

This project is licensed under the MIT License - see the LICENSE.md file for details