You can use Faye clients on the server side to send messages to in-browser clients or to other server-side processes.
How do I get this to work?
Here's my faye server code:
var http = require('http'),
faye = require('faye');
deflate = require('permessage-deflate');
port = 5500;
var server = http.createServer(),
bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 45});
bayeux.addWebsocketExtension(deflate);
bayeux.attach(server);
server.listen(port);
bayeux.getClient().subscribe('/test/*', function(message) {
console.log('[' + JSON.stringify(message) + ']: ');
});
bayeux.on('subscribe', function(clientId, channel) {
console.log('[ SUBSCRIBE] ' + clientId + ' -> ' + channel);
});
bayeux.on('unsubscribe', function(clientId, channel) {
console.log('[UNSUBSCRIBE] ' + clientId + ' -> ' + channel);
});
bayeux.on('disconnect', function(clientId) {
console.log('[ DISCONNECT] ' + clientId);
});
console.log('Listening on ' + port);
Listening on 5500
[ SUBSCRIBE] 6u6svqn0e490vtivgcmq4tjf2qar5j8 -> /test/*
const http = require('http')
const faye = require('faye')
const port = 3000
const server = http.createServer()
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
let eventBus = new faye.Client('http://localhost:5500/faye');
eventBus.connect()
var subscription = eventBus.subscribe('/test/*', function(message){
console.log("Event Received");
console.log(message);
})
//This is optional
subscription.then(function() {
console.log('Subscription is now active!');
});
setTimeout(function() {
console.log("publishing message");
var publication = eventBus.publish('/test/new', { text: 'test message'});
publication.then(function() {
console.log('Message received by server!');
}, function(error) {
console.log('There was a problem: ' + error.message);
});
},2000)