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

react-socket-context

v1.1.3

Published

react socket.io provider component, passing the socket to children using the context

Downloads

12

Readme

React Socket Context

Build Status Dependencies Dev Dependencies Peer Dependencies

A small component that creates a socket.io connection and exposes it via the components' context to child components.

How to install?

npm install --save react-socket-context

As you probably already have react (including react-dom) and socket.io in your dependencies, that's all you need. react-socket-context exposes those as peer dependencies.

Wait? There is react-socket?

react-socket is great if you just wanna listen to events that are streamed from the server to the client. As your component does never get direct access to the socket itself in your code, you can not easily emit events on the socket to pass messages to the server.

By exposing the socket directly though the context to all child components, you have direct access to it and can emit as well as subscribe to events.

So how do I use it?

Given this start:

import React from 'react';
import { render } from 'react-dom';

import SocketContext from 'react-socket-context';
import MyComponent from './MyComponent';

render(
  <SocketContext>
    <MyComponent foo="bar"/>
  </SocketContext>
    , document.getElementById('app')
);

This will set up a socket at the default URL (wherever your app was loaded from), and expose it in the child context for child components to access. To access it in MyComponent, do as follows:

import React, { Component, PropTypes } from 'react';

export default class MyComponent extends Component {

  contextTypes = {
    socket: PropTypes.object.isRequired,
  }

  componentDidMount() {
    this.context.socket.on('bootstrap', (data) => this.handleDataBootstrap(data));
    this.context.socket.on('event', (data) => this.handleDataIncremental(data));
    this.context.socket.emit('bootstrap', { duration: Moment.duration(1, 'h') } );
  }

  handleDataBootstrap(data) {
    // Handle your bootstrap data package to set up the component.
    this.setState({foo: data.foo});
  }

  handleDataIncremental(data) {
    // Merge the new event
    const newFoo = this.mergeFoo(this.state.foo, data);
    this.setState({foo: newFoo});
  }

  mergeFoo(base, increment) {
    // merge data
  }
  // ...
}

Looking at this component, it just accesses the provided socket via this.context.socket. For that to work, you need to declare your usage of the socket in contextTypes.

Once you did that, you should easily be able to work with your socket inside your component, either subscribing to events, or emitting events of your own.