diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c41fee5 --- /dev/null +++ b/.babelrc @@ -0,0 +1,6 @@ +{ + "plugins": [ + "transform-es2015-modules-commonjs", + "transform-es2015-arrow-functions" + ] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c563b71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules +build +logs +*.log diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..1a3fbaa --- /dev/null +++ b/.npmignore @@ -0,0 +1,6 @@ +node_modules +lib +test +logs +*.log +.babelrc diff --git a/README.md b/README.md new file mode 100644 index 0000000..5da44c4 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# socket.io-redux +Redux middleware to emit action via socket.io. + +## API +```javascript +import io from 'socket.io-client'; +import { createStore, applyMiddleware } from 'redux'; + +import socketIO from 'socket.io-redux'; + +import reducer from './reducer'; + +const store = createStore(reducer, applyMiddleware( + socketIO(io.connect(process.env.NODE_ENV)) +)); +``` +* `socketIO` receive a `socket` instance created by `io.connect(url)`. diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..cb3a6f4 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,9 @@ +const socketIO = socket => () => next => action => { + if (action.meta && action.meta.socket && action.meta.socket.channel) { + socket.emit(action.meta.socket.channel, action); + } + + return next(action); +} + +export default socketIO; diff --git a/package.json b/package.json new file mode 100644 index 0000000..bf05d29 --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "socket.io-redux", + "version": "1.0.0", + "description": "Redux middleware to emit action via socket.io.", + "main": "build/index.js", + "directories": { + "test": "test" + }, + "scripts": { + "build": "babel lib --out-dir build", + "pretest": "npm run build", + "prepublish": "npm run test", + "test": "babel-node test/index.js | tap-spec" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/sergiodxa/socket.io-redux.git" + }, + "keywords": [ + "redux", + "middleware", + "socket.io" + ], + "author": "Sergio Daniel Xalambrí (http://sergio.xalambri.com.ar/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/sergiodxa/socket.io-redux/issues" + }, + "homepage": "http://sergio.xalambri.com.ar/socket.io-redux", + "devDependencies": { + "babel": "6.5.2", + "babel-cli": "6.6.5", + "babel-plugin-transform-es2015-arrow-functions": "6.5.2", + "babel-plugin-transform-es2015-modules-commonjs": "6.7.0", + "tap-spec": "4.1.1", + "tape": "4.5.1" + } +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..ce108d8 --- /dev/null +++ b/test/index.js @@ -0,0 +1,27 @@ +import test from 'tape'; +import socketIO from '../build/index'; + +function next(action) { return action; } + +test('socket.io middleware', t => { + t.plan(2); + + const testAction = { + type: 'ADD_NEW', + payload: 'hello world!', + meta: { + socket: { + channel: 'add:new', + }, + }, + }; + + const socket = { + emit(channel, data) { + t.equals(channel, 'add:new', 'it should have the channel "add:new"'); + t.deepEquals(data, testAction, 'it should have the action as data'); + } + } + + socketIO(socket)()(next)(testAction); +});