Skip to content

Instantly share code, notes, and snippets.

@olaferlandsen
Forked from daffl/app.js
Created January 14, 2019 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olaferlandsen/f62934e4d8593820874eb95f2d762a0f to your computer and use it in GitHub Desktop.
Save olaferlandsen/f62934e4d8593820874eb95f2d762a0f to your computer and use it in GitHub Desktop.
Feathers Buzzard improved real-time event filtering dispatchers
app.service('messages').dispatch('eventname', (message, hook) => {
// Just dispatch to one user
if(message.isPrivate) {
return app.channel(message.receiver_id);
}
// Returning falsy or nothing will do nothing
});
// Send to a certain room
app.service('messages').dispatch('eventname', (message, hook) => {
return app.channel(`rooms/${message.roomId}`);
});
// EVERYONE
app.service('messages').dispatch('eventname', (message, hook) => {
return app.channel(app.channels);
});
// Filter connections manually, e.g. if the connection user and message user are friends
// This works similar to the old event filters
app.service('messages').dispatch('eventname', (message, hook) => {
return app.channel(app.channels).filter(connection => connection.user.friends.indexOf(message.user) !== -1);
});
// Modify the data that are sent to the channel for that event
app.service('messages').dispatch('eventname', (message, hook) => {
const modifiedMessage = cloneAndModify(message);
return app.channel(`rooms/${message.roomId}`, `rooms/general`).send(modifiedMessage);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment