Skip to content
This repository has been archived by the owner on Jul 18, 2018. It is now read-only.

Use Halley for realtime communications instead of Faye #17

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.swo
node_modules
npm-debug.log
.env
83 changes: 41 additions & 42 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* jshint node:true, unused:true */

var https = require('https');
var http = require('http');
var qs = require('qs');
var Q = require('q');
var url = require('url');
var debug = require('debug')('node-gitter');
var https = require('https');
var http = require('http');
var qs = require('qs');
var Promise = require('bluebird');
var url = require('url');
var debug = require('debug')('node-gitter');

var Client = function(token, opts) {
opts = opts || {};
Expand Down Expand Up @@ -44,7 +44,6 @@ var Client = function(token, opts) {
Client.prototype.request = function(method, path, opts) {
opts = opts || {};
var self = this;
var defer = Q.defer();

var headers = {
'Authorization': 'Bearer ' + this.token,
Expand All @@ -67,47 +66,47 @@ Client.prototype.request = function(method, path, opts) {

debug('%s %s', req_opts.method, req_opts.path);

var req = scheme.request(req_opts, function(res) {
// Accommodate webpack/browserify
if(res.setEncoding) {
res.setEncoding('utf-8');
}

self.rateLimit = res.headers['x-ratelimit-limit'];
self.remaining = res.headers['x-ratelimit-remaining'];

var data = '';
res.on('data' , function(chunk) {
data += chunk;
});

res.on('end', function() {
var body;
try {
body = JSON.parse(data);
} catch(err) {
defer.reject(new Error(res.statusCode + ' ' + data));
return new Promise(function(resolve, reject) {
var req = scheme.request(req_opts, function(res) {
// Accommodate webpack/browserify
if(res.setEncoding) {
res.setEncoding('utf-8');
}

if (res.statusCode !== 200) {
defer.reject(new Error(res.statusCode + ' ' + data));
} else {
defer.resolve(body);
}
self.rateLimit = res.headers['x-ratelimit-limit'];
self.remaining = res.headers['x-ratelimit-remaining'];

var data = '';
res.on('data' , function(chunk) {
data += chunk;
});

res.on('end', function() {
var body;
try {
body = JSON.parse(data);
} catch(err) {
reject(new Error(res.statusCode + ' ' + data));
}

if (res.statusCode !== 200) {
reject(new Error(res.statusCode + ' ' + data));
} else {
resolve(body);
}
});
});
});

req.on('error', function(err) {
defer.reject(err);
});

if (opts.body) {
req.write(JSON.stringify(opts.body));
}
req.on('error', function(err) {
reject(err);
});

req.end();
if (opts.body) {
req.write(JSON.stringify(opts.body));
}

return defer.promise;
req.end();
});
};

Client.prototype.stream = function(path, cb) {
Expand Down
21 changes: 15 additions & 6 deletions lib/faye.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* jshint node:true, unused:true */
'use strict';

var Faye = require('gitter-faye');
var Halley = require('halley');
var EventEmitter = require('eventemitter3');
var Promise = require('bluebird');

// Authentication extension for Faye
var ClientAuthExt = function(opts) {
Expand Down Expand Up @@ -41,7 +43,7 @@ var FayeClient = function(token, opts) {

this.subscriptions = {};

this.client = new Faye.Client(host, {timeout: 60, retry: 5, interval: 1});
this.client = new Halley.Client(host);
this.client.addExtension(new ClientAuthExt({token: token, clientType: opts.clientType}));
this.client.addExtension(new SnapshotExt({subscriptions: this.subscriptions}));
};
Expand All @@ -52,6 +54,8 @@ FayeClient.prototype.subscribeTo = function(resource, eventName) {
var emitter = new EventEmitter();
var subscription = this.client.subscribe(resource, function(msg) {
emitter.emit(eventName, msg);
}).catch(function(err) {
emitter.emit('error', err);
});

this.subscriptions[resource] = {
Expand All @@ -66,10 +70,15 @@ FayeClient.prototype.subscribeTo = function(resource, eventName) {
FayeClient.prototype.disconnect = function() {
var self = this;

Object.keys(this.subscriptions).forEach(function(sub) {
self.subscriptions[sub].subscription.cancel();
self.subscriptions[sub].emitter.removeAllListeners();
});
Object.keys(this.subscriptions)
.forEach(function(sub) {
var subscriptionsInfo = self.subscriptions[sub];
subscriptionsInfo.emitter.removeAllListeners();
});

return this.client.disconnect();


};

module.exports = FayeClient;
1 change: 1 addition & 0 deletions lib/gitter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* jshint node:true, unused:true */
'use strict';

var Client = require('./client.js');
var Users = require('./users.js');
Expand Down
21 changes: 14 additions & 7 deletions lib/rooms.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* jshint node:true, unused:true */
'use strict';

var util = require('util');
var EventEmitter = require('eventemitter3');
var Q = require('q');
var Promise = require('bluebird');

var Room = function(attrs, client, faye) {
EventEmitter.call(this);
Expand All @@ -18,8 +19,8 @@ var Room = function(attrs, client, faye) {
if (typeof this.users !== 'function') {
var users = this.users;
this.users = function() {
return Q.resolve(users);
}
return Promise.resolve(users);
};
}

this.client = client;
Expand Down Expand Up @@ -81,17 +82,23 @@ Room.prototype.subscribe = function() {
var resourcePath = '/api/v1/rooms/' + this.id + '/' + resource;
var events = this.faye.subscribeTo(resourcePath, resourcePath)
events.on(resourcePath, function(msg) {
this.emit(resource, msg);
this.emit(resource, msg);
}.bind(this));
}.bind(this));
};

Room.prototype.unsubscribe = function() {
['chatMessages', 'events', 'users'].forEach(function(resource) {
var promises = Promise.all(['chatMessages', 'events', 'users'].forEach(function(resource) {
var resourcePath = '/api/v1/rooms/' + this.id + '/' + resource;
var meta = this.faye.subscriptions[resourcePath];
if (meta) meta.subscription.cancel();
}.bind(this));
if (meta) {
meta.subscription.then(function(subscription) {
return subscription.unsubscribe();
});
}
}.bind(this)));

return Promise.all(promises)
};

// DEPRECATED Rooms is now an event emitter and all you need is to
Expand Down
8 changes: 4 additions & 4 deletions lib/users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* jshint node:true, unused:true */

var User = function(attrs, client, faye) {
// API path to Users
// API path to Users
this.path = '/user';

if (attrs)
Expand Down Expand Up @@ -32,7 +32,7 @@ User.prototype.findById = function(id, cb) {
.then(function(userData) {
return new User(userData, this.client, this.faye);
}.bind(this));

return cb ? user.nodeify(cb) : user;
};

Expand All @@ -42,7 +42,7 @@ User.prototype.findByUsername = function(username, cb) {
var userData = results.results[0];
return new User(userData, this.client, this.faye);
}.bind(this));

return cb ? user.nodeify(cb) : user;
};

Expand All @@ -55,7 +55,7 @@ User.prototype.findByUsername = function(username, cb) {
});

User.prototype.markAsRead = function(roomId, chatIds, cb) {
var resourcePath = this.path + '/' + this.id + '/troupes/' + roomId + '/unreadItems';
var resourcePath = this.path + '/' + this.id + '/rooms/' + roomId + '/unreadItems';
var resource = this.client.post(resourcePath, {body: {chat: chatIds}});
return cb ? resource.nodeify(cb) : resource;
};
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-gitter",
"version": "1.2.8",
"version": "2.0.3",
"description": "Gitter client",
"main": "lib/gitter.js",
"scripts": {
Expand All @@ -20,15 +20,16 @@
},
"homepage": "https://github.com/gitterHQ/node-gitter",
"dependencies": {
"debug": "^0.8.1",
"bluebird": "^3.1.1",
"debug": "^2.2.0",
"eventemitter3": "^0.1.6",
"faye": "~1.0.1",
"gitter-faye": "^1.1.0-e",
"halley": "^0.4.2",
"q": "~1.0.1",
"qs": "~1.2.1"
},
"devDependencies": {
"mocha": "~1.18.2",
"retire": "latest"
"mocha": "^2.3.4",
"retire": "^1.1.2"
}
}
10 changes: 5 additions & 5 deletions test/rooms-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ describe('Gitter Rooms', function() {
}).then(function(rooms) {
var check = rooms.some(function(room) { return room.name === 'node-gitter/yacht'; });
assert.equal(false, check);
}).fin(function() {
}).finally(function() {
// Join the room again for the rest of the tests
gitter.rooms.join('node-gitter/yacht');
}).nodeify(done);
});

it('should not be able to join an invalid room', function(done) {
gitter.rooms.join('some-invalid-room').then(function() {
}).fail(function(err) {
}).catch(function(err) {
assert(err);
done();
}).fail(done);
}).catch(done);
});

it('should be able to send a message', function(done) {
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('Gitter Rooms', function() {
});

setTimeout(function() { room.send(msg); }, 500);
}).fail(done);
}).catch(done);
});

it('should be able to subscribe to a room', function(done) {
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('Gitter Rooms', function() {
});

setTimeout(function() { room.send(msg); }, 750);
}).fail(done);
}).catch(done);
});


Expand Down
2 changes: 1 addition & 1 deletion test/users-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Gitter Users', function() {
it('should fail when fidning an invalid user', function(done) {
gitter.users.find('invalid').then(function() {
assert(false);
}).fail(function() {
}).catch(function() {
done();
});
});
Expand Down