Skip to content

Instantly share code, notes, and snippets.

@jermsam
Created January 12, 2019 05:37
Show Gist options
  • Save jermsam/cc1a7cfe99509a5ccafbf1ef4a563146 to your computer and use it in GitHub Desktop.
Save jermsam/cc1a7cfe99509a5ccafbf1ef4a563146 to your computer and use it in GitHub Desktop.
My channel gist
module.exports = function(app) {
if (typeof app.channel !== 'function') {
// If no real-time functionality has been configured just return
return;
}
app.on('connection', connection => {
// On a new real-time connection, add it to the anonymous channel
app.channel('anonymous').join(connection);
});
/* eslint-disable no-console */
app.on('login', (authResult, { connection }) => {
// connection can be undefined if there is no
// real-time connection, e.g. when logging in via REST
if (connection) {
// Obtain the logged in user from the connection
const user = connection.user;
console.log('loged in as ', user.firstname);
// The connection is no longer anonymous, remove it
app.channel('anonymous').leave(connection);
// Add it to the authenticated user channel
app.channel('authenticated').join(connection);
// Channels can be named anything and joined on any condition
// E.g. to send real-time events only to admins use
// if(user.isAdmin) { app.channel('admins').join(connection); }
// If the user has joined e.g. chat rooms
// if(Array.isArray(user.rooms)) user.rooms.forEach(room => app.channel(`rooms/${room.id}`).join(channel));
// Easily organize users by email and userid for things like messaging
// app.channel(`emails/${user.email}`).join(channel);
// app.channel(`userIds/$(user.id}`).join(channel);
// If a conversation is started
if (Array.isArray(user.messages))
user.messages.forEach(message => {
const conversationsWithThatMessage = app.service('conversations').find({
query: {
messageId: message.id,
},
});
const messageReceivers = conversationsWithThatMessage.map(m => m.receiverId);
if (message.senderId === user.id || messageReceivers.includes(user.id)) {
app.channel(`messages/${message.id}`).join(connection);
}
});
}
});
app.on('logout', (payload, { connection }) => {
if (connection) {
//When logging out, leave all channels before joining anonymous channel
// Obtain the logged in user from the connection
const user = connection.user;
console.log(user.firstname, ' loged out. ');
app.channel(app.channels).leave(connection);
app.channel('anonymous').join(connection);
}
});
// eslint-disable-next-line no-unused-vars
app.publish((data, hook) => {
// Here you can add event publishers to channels set up in `channels.js`
// To publish only for a specific event use `app.publish(eventname, () => {})`
console.log(
'Publishing all events to all authenticated users. See `channels.js` and https://docs.feathersjs.com/api/channels.html for more information.'
); // eslint-disable-line
// e.g. to publish all service events to all authenticated users use
return app.channel('authenticated');
});
// Here you can also add service specific event publishers
// e.g. the publish the `users` service `created` event to the `admins` channel
// app.service('users').publish('created', () => app.channel('admins'));
app.service('users').publish('patched', () => app.channel('authenticated', 'anonymous'));
app.service('messages').publish('created', () => {
console.log('message is created');
// app.channel('authenticated', 'anonymous');
});
// With the userid and email organization from above you can easily select involved users
// app.service('messages').publish(() => {
// return [
// app.channel(`userIds/${data.createdBy}`),
// app.channel(`emails/${data.recipientEmail}`)
// ];
// });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment