# gcacace-redux-socket.io

> An opinionated connector between socket.io and redux

Latest version **1.0.1** (published 2016-03-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install gcacace-redux-socket.io
pnpm add gcacace-redux-socket.io
yarn add gcacace-redux-socket.io
bun add gcacace-redux-socket.io
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-03-26 |
| First published | 2016-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | gianluca.cacace@gmail.com |
| Maintainers | gcacace |
| Keywords | redux, middleware, socket.io, action |

## Links

- npm: https://www.npmjs.com/package/gcacace-redux-socket.io
- Repository: https://github.com/gcacace/redux-socket.io
- Homepage: https://github.com/gcacace/redux-socket.io#readme
- Issues: https://github.com/gcacace/redux-socket.io/issues
- npm.io page: https://npm.io/package/gcacace-redux-socket.io

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2016-03-26

## README

# gcacace-redux-socket.io
An enhanced version of the opinionated connector between socket.io and Redux `redux-socket.io`.

Philosophy
-------------
### Client → Server
Messages are sent to the server by dispatching actions to Redux store, where the action `type` has a specific prefix, `toSocket/` as default.

### Server → Client
Messages are listened from the server by dispatching actions to Redux store, where the action `type` has a specific prefix, `fromSocket/` as default.

> **Note:** This middleware must be instructed with the list of events coming from the server.

How to use
-------------
### Installation
```
npm install --save git+https://git@github.com/gcacace/redux-socket.io#1.0.0
```

### Example usage
This will create a middleware that sends actions to the server when the action type starts with `toSocket/`.
When the socket.io client receives a message with event name starting with `fromSocket/`, it will dispatch the action to the store.

The result of running this code from the client is a request to the server and a response from the server, both of
which go through the redux store's dispatch method.

####Client side:
```js
import { createStore, applyMiddleware } from 'redux';
import createSocketIoMiddleware from 'redux-socket.io';
import io from 'socket.io-client';
let socket = io('http://localhost:3000');
let socketIoMiddleware = createSocketIoMiddleware(socket, 'fromSocket/', 'toSocket/', ['fromSocket/pong']);

function reducer(state = {}, action) {
  switch(action.type) {
  
    // This event is dispatched from the redux-socket.io middleware only if listed into the configuration array
    // defined above (4th argument of the `createSocketIoMiddleware` function
    case 'fromSocket/pong':
      return Object.assign({}, { message: action.payload });
      
    default:
      return state;
  }
}
let store = applyMiddleware(socketIoMiddleware)(createStore)(reducer);

// This will send a message to the socket.io server, with event name `ping` and data `This is a ping!`
store.dispatch({type: 'toServer/ping' , payload: 'This is a ping!' });
```

####Server side:
```js
var http = require('http');
var server = http.createServer();
var socket_io = require('socket.io');
server.listen(3000);
var io = socket_io();
io.attach(server);

io.on('connection', function(socket) {
  console.log("Socket connected: " + socket.id);
  
  socket.on('ping', (message) => {
    console.log('Got ping data!', message);
    socket.emit('pong', message);
  });
});
```

License
-------------
Copyright © 2016 Gianluca Cacace

Copyright for portions of this project are held by Ian Taylor, 2015.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

---
_Source: https://npm.io/package/gcacace-redux-socket.io · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
