Yet Another Redux Middleware for websocket. Websocket Middleware enable simple to use websocket with Redux. This middleware use action to dispatch actions with Redux and interact with WebSocket. Dispatch action in Redux directly from server.
If you had to do with Websockets and Redux, you will have surely tried some and you have noticed that some features is missing. This library ecloses all features that you may need in WebSocket used with Redux.
It's dependences free and have <10k size.
The middleware provide an auto reconnect system when websocket is closed by error. By default encode and decode JSON message. If you want dispatch directly actions from server in Redux. If you send JSON in Action pattern the middleware dispatch it.
Support multi WebSockets for use Redux with different channels.
Other important features missing in other library is heartbeat for avoid websocket timeout disconnect. We do it :D.
Other things? It have a beautiful name!
- Creating and opening the WebSocket for multiple endpoints
- Autoreconnect on websocket connection closed
- Heartbeat message
- Customizable heartbeat message and interval.
- Encoding/Decoding JSON Messages
- Dispatch Redux Action from Server
npm install --save fp-redux-websocket
Install as similar redux middleware:
import { applyMiddleware, createStore } from 'redux';
import { createFPWebSocketMiddleware } from 'fp-redux-websocket';
const wsProps = {
// See Options paragraph
}
const webSocketMiddleware = createFPWebSocketMiddleware(wsProps);
const store = createStore(reducer, applyMiddleware(webSocketMiddleware))
Param | Description | Type | Default |
---|---|---|---|
heartbeat | Enable heartbeat message. | boolean | false |
heartbeatMessage | Heartbeat message sendend each interval if heartbeat is true. | string | ---heartbeat--- |
heartbeatInterval | Interval beetween two message for send heartbeat. If websocket send or receive a message, the interval is reset. In milliseconds. | number | 15000 |
autoReconnect | Enable autoreconnect if websocket is closed for any reason different from user closed. | boolean | false |
intervalReconnect | Interval beetween last closed (user closing excluded) and next reconnect, if autoReconnect is enabled. In milliseconds. | number | 5000 |
useMessageAsReduxAction | If websocket receive a message in action pattern, dispatch it on redux. | boolean | false |
The following actions are available in middleware. All actions would be caught by reducers or other middleware.
// These message are user-generated
WEBSOCKET_CONNECT
WEBSOCKET_DISCONNECT
WEBSOCKET_SEND_MESSAGE
// These messages are generated by WebSocket
WEBSOCKET_CONNECTING
WEBSOCKET_OPEN
WEBSOCKET_CLOSED
WEBSOCKET_MESSAGE_RECEIVED
WEBSOCKET_ERROR
There is two action creator for send message:
sendMessage
for sending string messagesendObject
for sending object message (this parse object to string with JSON)
You can import ACTION and ActionCreator from standar library
import { sendObject, WEBSOCKET_OPEN, wsConnect } from 'fp-redux-websocket';
That actions can be dispatched from user
Open a connection to a WebSocket
wsConnect(url)
Close a connection to a WebSocket
wsDisconnect(url?)
If url
is null
, middleware close the first (or unique) WebSocket opened
Send message to websocket
sendMessage(string,url?)
Send a string to websocket. If url
is null, send message to unique WebSocket opened.
sendObejct(obejct,url?)
Send an object to websocket. If url
is null, send object to unique WebSocket opened.
Websocket actions correspond to the callbacks on a WebSocket object.
After WEBSOCKET_CONNECT
in connecting phase.
{
type: WEBSOCKET_CONNECTING,
timeStamp: Date,
websocket: WebSocket,
}
Dispatched from redux-websocket when the WebSocket onopen
callback is executed.
{
type: WEBSOCKET_OPEN,
timeStamp: Date,
websocket: WebSocket,
}
Dispatched from redux-websocket when the WebSocket onclosed
callback is executed.
{
type: WEBSOCKET_CLOSED,
reason: String,
timeStamp: Date,
websocket: WebSocket,
}
When WebSocket received message
{
type: WEBSOCKET_MESSAGE_RECEIVED,
message: String,
timeStamp: Date,
websocket: WebSocket,
}
When WebSocket throw error
{
type: WEBSOCKET_ERROR,
message: String,
timeStamp: Date,
url: String,
}
All main changelog is reporting in CHANGELOG.md
Pasqualino de Simone @pasalino
Thanks to Federico Blancato who contributed to the initial version of this library
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
If you like this library and would like to make modifications or addition, please fork repo and issue a pull request. Here a couple things that we know are needed.
- Better test coverage
- Create example for use in React
- Create example for use with Server (Node.js, Ruby, Django)
- Create example for use multisocket
We use SemVer for versioning. For the versions available, see the Tags.
This project is licensed under the MIT License - see the LICENSE.md file for details.