├── Makefile ├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── package.json ├── README.md ├── index.js └── test └── index.js /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha 4 | 5 | .PHONY: test 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6" 4 | - "7" 5 | - "8" 6 | - "9" 7 | - "10" 8 | - "node" 9 | - "lts/*" 10 | 11 | matrix: 12 | fast_finish: true 13 | 14 | services: 15 | - mongodb 16 | 17 | before_script: 18 | - sleep 15 19 | - mongo test --eval 'db.createUser({user:"test",pwd:"test",roles:["readWrite"]});' 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "socket.io-adapter-mongo", 3 | "version": "2.0.5", 4 | "description": "", 5 | "scripts": { 6 | "test": "mocha" 7 | }, 8 | "dependencies": { 9 | "debug": "2.6.9", 10 | "mongodb-uri": "^0.9.7", 11 | "msgpack-js": "0.3.0", 12 | "mubsub": "^1.4.0", 13 | "socket.io-adapter": "^1.1.1", 14 | "uid2": "0.0.3" 15 | }, 16 | "devDependencies": { 17 | "async": "0.2.10", 18 | "expect.js": "0.3.1", 19 | "mocha": "1.18.0", 20 | "socket.io": "^2.0.3", 21 | "socket.io-client": "^2.0.3" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/lklepner/socket.io-adapter-mongo.git" 26 | }, 27 | "license": "MIT", 28 | "contributors": [ 29 | { 30 | "name": "Sam Workman", 31 | "email": "sam@mod.it" 32 | }, 33 | { 34 | "name": "Shannon Poole", 35 | "email": "shannon@mod.it" 36 | }, 37 | { 38 | "name": "Isaac Johnson", 39 | "email": "iojohnso@gmail.com" 40 | }, 41 | { 42 | "name": "Chris Whitten", 43 | "email": "christopherwhitten@gmail.com" 44 | }, 45 | { 46 | "name": "Yaw Etse", 47 | "email": "yaw.etse@gmail.com" 48 | }, 49 | { 50 | "name": "Lou Klepner", 51 | "email": "lklepner@gmail.com" 52 | } 53 | ] 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # socket.io-adapter-mongo 2 | 3 | [![Build Status](https://secure.travis-ci.org/modit/socket.io-adapter-mongo.png)](http://travis-ci.org/modit/socket.io-adapter-mongo) 4 | [![NPM version](https://badge.fury.io/js/socket.io-adapter-mongo.png)](http://badge.fury.io/js/socket.io-adapter-mongo) 5 | 6 | *This module is modified from [socket.io-redis](https://github.com/Automattic/socket.io-redis) 7 | 8 | Update 5/31/2017 - Versions prior to 1.0 allowed an object to be passed which was used to build a URI. This caused problems when using replica sets and caused warnings when MongoDB change the client API. In the interest of simplicity and futureproofing the internal URI construction has been eliminated and it is now required that a valid mongo URI be passed. 9 | 10 | ## How to use 11 | 12 | ```js 13 | var io = require('socket.io')(3000); 14 | var mongoAdapter = require('socket.io-adapter-mongo'); 15 | io.adapter(mongoAdapter( 'mongodb://localhost:27017' )); 16 | ``` 17 | 18 | By running socket.io with the `socket.io-adapter-mongo` adapter you can run 19 | multiple socket.io instances in different processes or servers that can 20 | all broadcast and emit events to and from each other. 21 | 22 | 23 | ## API 24 | 25 | ### adapter(uri[, opts]) 26 | 27 | `uri` is a string that matches a mongodb connection string 28 | ``` 29 | mongodb://localhost:27017/test 30 | mongodb://user:pass@localhost:27017/test 31 | mongodb://user:pass@host1:27017,host2:27017,host3:27017/test 32 | ``` 33 | 34 | ### adapter(opts) 35 | 36 | The following options are allowed: 37 | 38 | - `key`: the name of the key to pub/sub events on as prefix (`socket.io`) 39 | - `socket`: unix domain socket to connect to mongo (`"/tmp/mongo.sock"`). Will 40 | be used instead of the mongo URI if specified. 41 | - `client`: optional, the mubsub client to publish events on 42 | 43 | If you decide to supply a client, make sure you use [mubsub](https://github.com/scttnlsn/mubsub) as a client or one with an equivalent API. 44 | 45 | ## License 46 | 47 | MIT 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies. 3 | */ 4 | 5 | var uid2 = require('uid2') 6 | , mubsub = require('mubsub') 7 | , msgpack = require('msgpack-js') 8 | , Adapter = require('socket.io-adapter') 9 | , debug = require('debug')('socket.io-mongo') 10 | , mongodbUri = require('mongodb-uri'); 11 | ; 12 | 13 | /** 14 | * Module exports. 15 | */ 16 | 17 | module.exports = adapter; 18 | 19 | /** 20 | * Returns a mongo Adapter class. 21 | * 22 | * @param {String} mongo uri 23 | * @return {Mongo} adapter 24 | * @api public 25 | */ 26 | 27 | function adapter(uri, opts) { 28 | opts = opts || {}; 29 | 30 | // opts 31 | var socket = opts.socket; 32 | var client = opts.client; 33 | var key = opts.key || 'socket.io'; 34 | delete opts.key; // prevent key from being passed to mongoDB (via mubsub) and generating a warning 35 | 36 | // init clients if needed 37 | if (!client) client = socket ? mubsub(socket) : mubsub(uri, opts); 38 | 39 | // this server's key 40 | var uid = uid2(6); 41 | 42 | var channel = client.channel(key); 43 | 44 | /** 45 | * Adapter constructor. 46 | * 47 | * @param {String} namespace name 48 | * @api public 49 | */ 50 | 51 | function Mongo(nsp) { 52 | Adapter.call(this, nsp); 53 | 54 | channel.subscribe(key, this.onmessage.bind(this)); 55 | } 56 | 57 | /** 58 | * Inherits from `Adapter`. 59 | */ 60 | 61 | Mongo.prototype.__proto__ = Adapter.prototype; 62 | 63 | /** 64 | * Called with a subscription message 65 | * 66 | * @api private 67 | */ 68 | 69 | Mongo.prototype.onmessage = function (msg) { 70 | if (uid == msg.uid || !msg.uid) return debug('ignore same uid'); 71 | 72 | var args = msgpack.decode(msg.data.buffer); 73 | if (args[0] && args[0].nsp === undefined) 74 | args[0].nsp = '/'; 75 | 76 | if (!args[0] || args[0].nsp != this.nsp.name) return debug('ignore different namespace'); 77 | args.push(true); 78 | this.broadcast.apply(this, args); 79 | }; 80 | 81 | /** 82 | * Broadcasts a packet. 83 | * 84 | * @param {Object} packet to emit 85 | * @param {Object} options 86 | * @param {Boolean} whether the packet came from another node 87 | * @api public 88 | */ 89 | 90 | Mongo.prototype.broadcast = function (packet, opts, remote) { 91 | Adapter.prototype.broadcast.call(this, packet, opts); 92 | 93 | if (!remote) { 94 | channel.publish(key, { uid: uid, data: msgpack.encode([packet, opts]) }); 95 | } 96 | }; 97 | 98 | return Mongo; 99 | 100 | } 101 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var http = require('http').Server; 2 | var io = require('socket.io'); 3 | var ioc = require('socket.io-client'); 4 | var expect = require('expect.js'); 5 | var mubsub = require('mubsub') 6 | var adapter = require('../'); 7 | 8 | describe('socket.io-mongo', function(){ 9 | beforeEach(function(done){ //initialize collection 10 | var cli = mubsub('mongodb://test:test@localhost:27017/test'); 11 | var channel = cli.channel('socket.io'); 12 | channel.publish('socket.io', 'init', done); 13 | }); 14 | 15 | it('broadcasts', function (done){ 16 | this.timeout(5000); 17 | 18 | create(function(server1, client1){ 19 | create(function(server2, client2){ 20 | client1.on('woot', function(a, b){ 21 | expect(a).to.eql([]); 22 | expect(b).to.eql({ a: 'b' }); 23 | done(); 24 | }); 25 | server2.on('connection', function(c2){ 26 | c2.broadcast.emit('woot', [], { a: 'b' }); 27 | }); 28 | }); 29 | }); 30 | }); 31 | 32 | it('broadcasts to rooms', function(done){ 33 | this.timeout(5000); 34 | 35 | create(function(server1, client1){ 36 | create(function(server2, client2){ 37 | create(function(server3, client3){ 38 | server1.on('connection', function(c1){ 39 | c1.join('woot'); 40 | }); 41 | 42 | server2.on('connection', function(c2){ 43 | // does not join, performs broadcast 44 | c2.on('do broadcast', function(){ 45 | c2.broadcast.to('woot').emit('broadcast'); 46 | }); 47 | }); 48 | 49 | server3.on('connection', function(c3){ 50 | // does not join, signals broadcast 51 | client2.emit('do broadcast'); 52 | }); 53 | 54 | client1.on('broadcast', function(){ 55 | setTimeout(done, 100); 56 | }); 57 | 58 | client2.on('broadcast', function(){ 59 | throw new Error('Not in room'); 60 | }); 61 | 62 | client3.on('broadcast', function(){ 63 | throw new Error('Not in room'); 64 | }); 65 | }); 66 | }); 67 | }); 68 | }); 69 | 70 | // create a pair of socket.io server+client 71 | function create(nsp, fn){ 72 | var srv = http(); 73 | var sio = io(srv ); 74 | sio.adapter(adapter('mongodb://test:test@localhost:27017/test')); 75 | srv.listen(function(err){ 76 | if (err) throw err; // abort tests 77 | if ('function' == typeof nsp) { 78 | fn = nsp; 79 | nsp = ''; 80 | } 81 | nsp = nsp || '/'; 82 | var addr = srv.address(); 83 | var url = 'http://localhost:' + addr.port + nsp; 84 | fn(sio.of(nsp), ioc(url)); 85 | }); 86 | } 87 | 88 | }); --------------------------------------------------------------------------------