Skip to content

Instantly share code, notes, and snippets.

@daffl
Created June 28, 2017 15:06
Show Gist options
  • Save daffl/93d08c8f8a564345a9c0d97b7c3eecf3 to your computer and use it in GitHub Desktop.
Save daffl/93d08c8f8a564345a9c0d97b7c3eecf3 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