Resocket is a socket.io
wrapper and middleware for React
and Redux
applications.
It helps you abstract away all the socket.io
implementations in your app.
You can use Resocket separately without using the createResocketMiddleware
to have more control over the socket implementation.
npm install --save resocket
Using socket.io
with React-redux
applications does not allow you to much abstraction and increases complexity.
With Resocket, it provides you with a bit abstraction and leads to a cleaner code.
If you want more abstraction and allow Resocket middleware to do more of the stuff,
all you need is to import createResocketMiddleware
. Resocket only takes a few minutes to get started with.
Middleware
import Resocket, { createResocketMiddleware } from 'resocket';
const socket = Resocket.connect(url, opts);
const resocketMiddleware = createResocketMiddleware(socket, listOfEventsToEmitTo);
const middleware = applyMiddleware(thunk, resocketMiddleware);
Resocket
All you need to do is to call the connect
method on Resocket and use anywhere across your React-redux app.
import { render } from 'react-dom';
import Resocket from 'resocket'
Resocket.connect(url);
//...
render();
- url: Any url for establishing a socket connection e.g:
http://localhost:9000
- opts:
optional
These are the default optional parameters. Default parameters are:
auth: true,
reconnection: true,
reconnectionDelay: 1000
You can override them simply by passing for example:
Resocket.connect('http://localhost:3000', {auth: false});
You can also extend the options object with socket.io accepted parameters:
Resocket.connect('http://localhost:3000', {transports: ['polling']});
Note: On passing auth: false
Resocket won't allow a connection to be established. This is useful when you want to allow a connection to be only built after Login or some basic authentication.
No other than socket.io
and of course to React
and Redux
MIT