hexnut-with-observable
is a hexnut middleware for integrating with rxjs
.
Note: Requires peer dependency of rxjs ^v6.0.0
.
npm i hexnut-with-observable
const Hexnut = require('hexnut');
const {withObservable, filterMessages} = require('hexnut-with-observable');
const { tap, filter } = require('rxjs/operators');
const { pipe } = require('rxjs');
const app = new Hexnut({ port: 8181 });
app.use(withObservable(pipe(
// Do setup operations here...
tap(({ctx}) => {
if (ctx.isConnection) {
console.log(`[${ctx.ip}] Connection started`);
}
}),
// Filter to only messages
filterMessages(msg => msg !== 'skip'),
// Process messages
tap(({ctx}) => {
ctx.send(`You sent: ${ctx.message}`);
})
)));
app.start();
filterMessages(predicateFn)
Filters to messages that pass the predicate function. Automatically calls the next()
middleware if event is not a message or does not pass.
app.use(withObservable(pipe(
filterMessages(msg => msg.startsWith('!')),
tap(({ctx}) => {
ctx.send('Only messages beginning with a ! get here');
})
)));
filterConnections
Filters connections. Automatically calls the next()
middleware if event is not a connection.
app.use(withObservable(pipe(
filterConnections,
tap(({ctx}) => {
ctx.send('This is a connection event.');
})
)));
filterCloses
Filters close events. Automatically calls the next()
middleware if event is not a close.
app.use(withObservable(pipe(
filterCloses,
tap(({ctx}) => {
console.log('Client disconnected');
})
)));