├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | *.log 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SC-Redis 2 | ====== 3 | 4 | This is a Redis adaptor for SocketCluster: http://socketcluster.io/ 5 | It allows you to interact with SocketCluster channels via Redis and vice-versa. 6 | 7 | This module is useful if you have a simple setup on a single host and just want to be able to synchronize pub/sub channels between SC and Redis - So if you publish a message to a channel in Redis, clients connected to SC which are subscribed to that channel will also get the message. 8 | 9 | This is not an ideal solution for scaling SC horizontally (unless you can modify it to work with Redis Cluster maybe). The recommended approach for scaling SC horizontally is now SCC https://github.com/SocketCluster/socketcluster/blob/master/scc-guide.md 10 | 11 | ## Install 12 | 13 | ```bash 14 | npm install sc-redis 15 | ``` 16 | 17 | Make sure that you have the latest version of SocketCluster installed. 18 | 19 | ## Usage 20 | 21 | Put the following code inside the SocketCluster sample app - Inside **broker.js**: 22 | 23 | ```js 24 | var scRedis = require('sc-redis'); 25 | 26 | module.exports.run = function (broker) { 27 | console.log(' >> Broker PID:', process.pid); 28 | scRedis.attach(broker); 29 | }; 30 | ``` 31 | 32 | You will need to provide some brokerOptions to SocketCluster (in **server.js**) - These will automatically be added as an options property on your broker object. 33 | Example (substitute with relevant values): 34 | 35 | ```js 36 | var socketCluster = new SocketCluster({ 37 | // ... 38 | brokerOptions: { 39 | host: '54.204.147.15', 40 | port: 6379 41 | } 42 | }); 43 | ``` 44 | 45 | Note that SC-Redis uses the Node Redis client to hook into a Redis server. 46 | Any option described here: https://github.com/mranney/node_redis#overloading can be provided as a broker option - In production you may want to provide the 'password' property. 47 | 48 | Feel free to modify server.js to get some of these options from the command line if appropriate (instead of having them hard-coded inside server.js). 49 | 50 | To test, you need to launch your Redis server (on the host and port you specified in brokerOptions). 51 | Then you need to launch your SC server using (make sure your Redis server is running before you launch your SC instance): 52 | 53 | ```bash 54 | node server 55 | ``` 56 | 57 | Open your browser window and connect to your SC server... By default it's at: http://localhost:8000/ - Then open the developer console. 58 | Note that your client will subscribe to a 'pong' channel on SocketCluster. SC-Redis will automatically handle all the synchronization work. 59 | 60 | On the host on which your Redis server is running, you can interact with it using: 61 | 62 | ```bash 63 | redis-cli 64 | ``` 65 | 66 | Then inside the Redis prompt, you can enter: 67 | 68 | ```bash 69 | publish pong 'o:{"a":123, "b":456}' 70 | ``` 71 | 72 | You should see the object appear in your browser's developer console (from SocketCluster). 73 | 74 | Note that SC-Redis messages always need to start with 'o:' (if the data is a JSON object) or 's:' (if data should be interpreted as a string). 75 | 76 | 77 | ## Contributing 78 | 79 | SC-Redis is currently 'experimental'. It still needs a bit of polishing before you can use it in production. 80 | TODO: 81 | - Better error logging (capture errors from Redis client and emit 'error' on broker object?) 82 | - Reconnect behavior (after Redis client connection drops out) - Not sure if this is necessary or Node Redis client already does that automatically? 83 | - Synchronize data with Redis - Not just pub/sub channels - Will need to make changes to nData (https://github.com/SocketCluster/ndata) to make this possible. 84 | 85 | Pull requests are welcome. 86 | 87 | 88 | ## License 89 | 90 | (The MIT License) 91 | 92 | Copyright (c) 2013-2015 TopCloud 93 | 94 | 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: 95 | 96 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 97 | 98 | 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. 99 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const redis = require('redis'); 2 | const assert = require('assert'); 3 | 4 | function assertBrokerOptions (brokerOptions) { 5 | assert(brokerOptions, '"brokerOptions" is required to create a Redis client with sc-redis'); 6 | assert(brokerOptions.host, '"brokerOptions.host" is required to create a Redis client with sc-redis'); 7 | assert(brokerOptions.port, '"brokerOptions.port" is required to create a Redis client with sc-redis'); 8 | } 9 | 10 | function throwMissingRedisClientError (clientName) { 11 | throw new Error('Missing "' + clientName + '" option. Both "pubClient" and "subClient" must be specified if passing your own clients.'); 12 | } 13 | 14 | module.exports.attach = function (broker, options) { 15 | options = options || {} 16 | 17 | const instanceId = broker.instanceId; 18 | var subClient = options.subClient; 19 | var pubClient = options.pubClient; 20 | 21 | if (!subClient && !pubClient) { 22 | const brokerOptions = broker.options.brokerOptions; 23 | assertBrokerOptions(brokerOptions); 24 | 25 | subClient = redis.createClient(brokerOptions.port, brokerOptions.host, brokerOptions); 26 | pubClient = redis.createClient(brokerOptions.port, brokerOptions.host, brokerOptions); 27 | } else if (!subClient && pubClient) { 28 | throwMissingRedisClientError("subClient"); 29 | } else if (subClient && !pubClient) { 30 | throwMissingRedisClientError("pubClient"); 31 | } 32 | 33 | broker.on('subscribe', subClient.subscribe.bind(subClient)); 34 | broker.on('unsubscribe', subClient.unsubscribe.bind(subClient)); 35 | broker.on('publish', function (channel, data) { 36 | if (data instanceof Object) { 37 | try { 38 | data = '/o:' + JSON.stringify(data); 39 | } catch (e) { 40 | data = '/s:' + data; 41 | } 42 | } else { 43 | data = '/s:' + data; 44 | } 45 | 46 | if (instanceId != null) { 47 | data = instanceId + data; 48 | } 49 | 50 | pubClient.publish(channel, data); 51 | }); 52 | 53 | var instanceIdRegex = /^[^\/]*\//; 54 | 55 | subClient.on('message', function (channel, message) { 56 | var sender = null; 57 | message = message.replace(instanceIdRegex, function (match) { 58 | sender = match.slice(0, -1); 59 | return ''; 60 | }); 61 | 62 | // Do not publish if this message was published by 63 | // the current SC instance since it has already been 64 | // handled internally 65 | if (sender == null || sender != instanceId) { 66 | var type = message.charAt(0); 67 | var data; 68 | if (type == 'o') { 69 | try { 70 | data = JSON.parse(message.slice(2)); 71 | } catch (e) { 72 | data = message.slice(2); 73 | } 74 | } else { 75 | data = message.slice(2); 76 | } 77 | broker.publish(channel, data); 78 | } 79 | }); 80 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sc-redis", 3 | "version": "0.2.0", 4 | "description": "Redis adapter for SocketCluster", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/SocketCluster/sc-redis.git" 12 | }, 13 | "keywords": [ 14 | "redis", 15 | "socketcluster", 16 | "sc" 17 | ], 18 | "author": "Jonathan Gros-Dubois", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/SocketCluster/sc-redis/issues" 22 | }, 23 | "homepage": "https://github.com/SocketCluster/sc-redis", 24 | "dependencies": { 25 | "redis": "2.4.x" 26 | } 27 | } 28 | --------------------------------------------------------------------------------