Server-side Node.js clients

53 views
Skip to first unread message

inuvat...@gmail.com

unread,
Nov 2, 2018, 11:09:57 AM11/2/18
to Faye users

Server-side Node.js clients

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);



It's starts up fine and dislays:

Listening on 5500
[  SUBSCRIBE] 6u6svqn0e490vtivgcmq4tjf2qar5j8 -> /test/*



Here's my test server process:
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)

It starts up fine, but the "test message" is never received by faye server. 
What am I missing?

Thanks
Inuvative

inuvat...@gmail.com

unread,
Nov 2, 2018, 11:13:43 AM11/2/18
to Faye users
Forgot to mention. I'm only trying to send messages to other server-side processes.
Reply all
Reply to author
Forward
0 new messages