Skip to content

fredjens/with-socketio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

With SocketIO

React Higher Order Component for SocketIO

NPM

Install

yarn add with-socketio socket.io-client

Usage

Use it like this:

import React, { Component } from 'react'
import WithSocket from 'with-socketio';

class MyComponent extends Component {
  render () {
    return (
      <MyComponent />
    )
  }
}

WithSocket(MyComponent, { url, channels });
  • url: The url to your SocketIO server (@string)
  • channels: The channles you want to listen to (@array)

Example:

WithSocket(MyComponent, {
  url: 'http://localhost:5000',
  channels: ['events', 'data', 'error']
});

You can then access the events socket at this.props.events

Props

This HoC adds the props to your component:

[channels]: Your channels (@obj)
error: Error message (@string)
emit: Emit a message (@func)

Emit message

You can also emit messages to a channel:

this.props.emit('channel-name', 'Hello, Channel!');

Made with Create React Library

License

MIT © fredjens

About

React Higher Order Component for SocketIO

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published