Skip to content

Commit 408b119

Browse files
committed
added mongodb credential options
1 parent c503ccb commit 408b119

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ process, you should use [socket.io-emitter](http:///github.com/Automattic/socket
2424

2525
### adapter(uri[, opts])
2626

27-
`uri` is a string like `localhost:27017` where your mongo server
28-
is located. For a list of options see below.
27+
`uri` is a string that matches a mongodb connection string
28+
```
29+
mongodb://localhost:27017
30+
mongodb://user:pass@localhost:27017/test
31+
localhost:27017
32+
```
2933

3034
### adapter(opts)
3135

@@ -35,6 +39,8 @@ The following options are allowed:
3539
- `host`: host to connect to mongo on (`localhost`)
3640
- `port`: port to connect to mongo on (`27017`)
3741
- `db`: db to use in mongo (`mubsub`)
42+
- `username`: username to connect to mongo with
43+
- `password`: password to connect to mongo with
3844
- `socket`: unix domain socket to connect to mongo (`"/tmp/mongo.sock"`). Will
3945
be used instead of the host and port options if specified.
4046
- `client`: optional, the mubsub client to publish events on

index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var uid2 = require('uid2')
1010
, debug = require('debug')('socket.io-mongo')
1111
;
1212

13+
var URI_MATCH = /(?:mongodb\:\/\/)?(?:(.*)\:(.*)\@)?(.*)\:(\d+)(?:\/(.*))?/i;
14+
1315
/**
1416
* Module exports.
1517
*/
@@ -34,22 +36,27 @@ function adapter(uri, opts){
3436
}
3537

3638
// handle uri string
39+
uri = (uri || '').match(URI_MATCH);
3740
if (uri) {
38-
uri = uri.split(':');
39-
opts.host = uri[0];
40-
opts.port = uri[1];
41+
opts.username = uri[1];
42+
opts.password = uri[2];
43+
opts.host = uri[3];
44+
opts.port = uri[4];
45+
opts.db = uri[5];
4146
}
4247

4348
// opts
4449
var socket = opts.socket;
50+
var creds = (opts.username && opts.password) ? opts.username + ':' + opts.password + '@' : '';
4551
var host = opts.host || '127.0.0.1';
4652
var port = Number(opts.port || 27017);
4753
var db = opts.db || 'mubsub';
54+
4855
var client = opts.client;
4956
var key = opts.key || 'socket.io';
5057

5158
// init clients if needed
52-
if (!client) client = socket ? mubsub(socket) : mubsub('mongodb://' + host + ':' + port + '/' + db);
59+
if (!client) client = socket ? mubsub(socket) : mubsub('mongodb://' + creds + host + ':' + port + '/' + db);
5360

5461
// this server's key
5562
var uid = uid2(6);
@@ -66,7 +73,6 @@ function adapter(uri, opts){
6673
function Mongo(nsp){
6774
Adapter.call(this, nsp);
6875

69-
var self = this;
7076
channel.subscribe(key, this.onmessage.bind(this));
7177
}
7278

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "socket.io-adapter-mongo",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "",
55
"scripts": {
66
"test": "mocha"

test/index.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,24 @@ function client(srv, nsp, opts){
2323

2424
describe('socket.io-mongo', function(){
2525
beforeEach(function(done){ //initialize collection
26-
var cli = mubsub('mongodb://localhost:27017/test');
26+
var cli = mubsub('mongodb://test:test@localhost:27017/test');
2727
var channel = cli.channel('socket.io');
2828
channel.publish('socket.io', 'init', done);
2929
});
3030

3131
describe('broadcast', function(){
3232
beforeEach(function(done){
33-
this.mubsubClients = [];
3433
var self = this;
35-
36-
async.times(2, function(n, next){
37-
var cli = mubsub('mongodb://localhost:27017/test');
34+
35+
var connectOpts = [
36+
'mongodb://test:test@localhost:27017/test',
37+
'test:test@localhost:27017/test',
38+
{ host: 'localhost', port: '27017', username: 'test', password: 'test', db: 'test'}
39+
];
40+
41+
async.times(3, function(n, next){
3842
var srv = http();
39-
var sio = io(srv, { adapter: mongoAdapter({ client: cli }) });
40-
self.mubsubClients.push(cli);
43+
var sio = io(srv, { adapter: mongoAdapter(connectOpts[n]) });
4144

4245
srv.listen(function(){
4346
['/', '/nsp'].forEach(function(name){
@@ -99,12 +102,6 @@ describe('socket.io-mongo', function(){
99102
});
100103
});
101104

102-
afterEach(function(){
103-
this.mubsubClients.forEach(function(cli){
104-
cli.close();
105-
});
106-
});
107-
108105
it('should broadcast from a socket', function(done){
109106
async.each(this.sockets.slice(1), function(socket, next){
110107
socket.on('broadcast', function(message){

0 commit comments

Comments
 (0)