Files
habitica/website/server/libs/api-v3/pusher.js
Matteo Pagliazzi 0880850408 Real-time Chat (#7664)
* feat(realtime-chat): add Pusher library to the server

* feat(realtime-chat): only for private groups

* feat(realtime-chat): add authentication endpoint for Pusher

* feat(realtime-chat): client proof of concept

* fix typo in apidoc

* feat(realtime-chat): redo authentication and write integration tests

* remove firebase code

* fix client side tests

* fix line ending in bower.json

* feat(realtime chat): use presence channels for parties, send events & disconnect clients if user leaves or is removed from party, automatically update UI

* pusher: enable all events in the background

* fix pusher integration tests
2016-07-02 15:17:24 +02:00

42 lines
1.1 KiB
JavaScript

import Pusher from 'pusher';
import nconf from 'nconf';
import { InternalServerError } from './errors';
const IS_PUSHER_ENABLED = nconf.get('PUSHER:ENABLED') === 'true';
let pusherInstance;
if (IS_PUSHER_ENABLED) {
pusherInstance = new Pusher({
appId: nconf.get('PUSHER:APP_ID'),
key: nconf.get('PUSHER:KEY'),
secret: nconf.get('PUSHER:SECRET'),
encrypted: true,
});
}
let api = {
// https://github.com/pusher/pusher-http-node#publishing-events
trigger (channel, event, data, socketId = null) {
if (!IS_PUSHER_ENABLED) return Promise.resolve(null);
return new Promise((resolve, reject) => {
pusherInstance.trigger(channel, event, data, socketId, (err, req, res) => {
if (err) {
reject(err);
} else {
resolve([req, res]);
}
});
});
},
// https://github.com/pusher/pusher-http-node#authenticating-private-channels
authenticate (...args) {
if (!IS_PUSHER_ENABLED) throw new InternalServerError('Pusher is not enabled.');
return pusherInstance.authenticate(...args);
},
};
module.exports = api;