Skip to content

Instantly share code, notes, and snippets.

@randomnerd
Created November 19, 2015 10:03
Show Gist options
  • Save randomnerd/4214c1dc00e51bce7e35 to your computer and use it in GitHub Desktop.
Save randomnerd/4214c1dc00e51bce7e35 to your computer and use it in GitHub Desktop.
import _ from 'lodash';
export function start(api, next) {
api.actions.addMiddleware({
name: 'addUserIdToConnection',
global: true,
priority: 21,
preProcessor(data, midNext) {
if (data.session) data.connection.userId = data.session.id;
midNext();
}
});
api._sendToUser = (userId, data) => {
let conns = _.filter(api.connections.connections, (i) => {
return i.userId == userId && i.canChat == true;
});
for (let conn of conns) { conn.sendMessage(data); }
};
api.sendToUser = (userId, data) => {
api.redis.doCluster('api._sendToUser', [userId, data]);
};
next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment