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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mesteche/react-socket

v2.0.0

Published

react-socket React component

Readme

react-socket

Travis npm package Coveralls

A react-redux inspired library to deal with websocket.
Discord

Installation

Using npm

npm i --save @mesteche/react-socket

Using yarn

yarn add @mesteche/react-socket

API

<Socket [id] [socket] [url] [protocol] [onopen] [onerror] [onmessage]>

Makes the socket available to the children, anywhere below in the tree.
An open <Socket> is required for being able to use connect() on sub-components.

Props

  • children the component down the tree. Must be an only child, if you need to have siblings, wrap them in a single component.
  • id if you want to have multiple socket connected at once, you must give them an id that will be used by connect() to identify the right socket.

If you want to instanciate the connection outside of the <Socket> component, you should pass the WebSocket instance as a prop:

  • socket An instance of WebSocket

Else the <Socket> component can create it with the following parameters as props:

  • url The url to connect to
  • protocol The protocol to use
  • onopen A function to call when the connection is established
  • onerror A function to call if an errors occurs
  • onmessage A function to call when a message is recieved (the message will be passed as an argument)

See WebSocket documentation

Example

// main.js
import React from 'react'
import {render} from 'react-dom'
import Socket from 'react-socket'

/* See connect() documentation below */
import Demo from './Demo.js'

render(
  <Socket url='ws://localhost:8080'>
    <Demo onWsMessage={msg => console.info('message recieved', msg)}>
      Send Message
    </Demo>
  </Socket>,
  document.querySelector('#app')
)

connect(mapSendToProps)

Connects a React component to a WebSocket. Similarly to react-redux's connect(), it doesn't modify the component passed to it but returns a new component connected to the WebSocket instead.

In addition to connect the component's props to the socket's send() method, the new component will expose a onWsMessage and socketId prop, for you to register a callback when a message is recieved from the server and to select the socket you want to connect to. For instance, if you already use react-redux, you can use use react-redux's connect() on this new component to dispatch an action when a message arrives.

Arguments

  • mapSendToProps(Object or Function):
    • If an object is passed, each key is assumed to be a prop of the component, and the correspondign values are assumed to be functions that returns the data to be sent to the server. Each function will be wrapped into a send() call to the server.
    • If a function is passed, it will be given the send() function of the socket. It is expected that it returns an object containning the props. It's up to you to use send() in these props.

connectBase(mapSendToProps, [config]])

Similar to connect(), except instead of exposing onWsMessage and socketId as props on the returned component, it accepts a config argument.

Arguments

Same as connect() plus :

  • [config](Object):
    • [onWsMessage](Function): This function will be called when a message is recieved from the server, with the message as parameter.
    • [socketId](String or Symbol): The id of the socket you're targetting.

Examples

mapSendToProps as an object

In this example, 'Message' will be sent to the server when the button is clicked.

// Demo.js
import React from 'react'
import { connect } from 'react-socket'

// Basic React component
export const Demo = ({sendMessage, children}) => (
  <button onClick={function () {sendMessage('Message')}}>
    {children}
  </button>
)

const mapSendToProps = {
  sendMessage: msg => msg,
}

export default connect(mapSendToProps)(Demo)
mapSendToProps as a function

This Example is similar to the previous one but mapSendToProps is used as a function.

// Demo.js
import React from 'react'
import { connect } from 'react-socket'

// Basic React component
export const Demo = ({sendMessage, children}) => (
  <button onClick={function () {sendMessage('Message')}}>
    {children}
  </button>
)

const mapSendToProps = send => ({
  sendMessage: msg => send(msg),
})

export default connect(mapSendToProps)(Demo)
usage with react-redux's connect

In this example, we use the onWsMessage prop of the connected component to listen to the server. We also use react-redux to respond to such event by dispatching an action (see redux and react-redux documentation).

// Demo.js
import React from 'react'
import { connect as wsConnect } from 'react-socket'
import { connect as RRConnect } from 'react-redux'

// Basic React component
export const Demo = ({sendMessage, children}) => (
  <button onClick={function () {sendMessage('Message')}}>
    {children}
  </button>
)

const mapSendToProps = {
  sendMessage: msg => msg,
}

const mapDispatchToProps = {
  onWsMessage: msg => ({
    type: 'SERVER_MESSAGE',
    payload: msg.data,
  })
}

export default RRConnect(null, mapDispatchToProps)(
  wsConnect(mapSendToProps)(Demo)
)