i am using node js and faye to simply pass some messages to clients ,
i create a node server
var http = require('http'),
faye = require('faye'),
url = require('url'),
qs = require('querystring');
var POST;
var bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 45});
function publish(request,response)
{
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
POST = qs.parse(body);
if(POST.secrete_key=="@#$werw*#@erwe*&^&*rw234234") // validate request using secret key
{
if(POST.root=="global"||POST.root=="web"){
bayeux.getClient().publish(POST.channelWeb,{text: POST.textWeb});
}
if(POST.root=="global"||POST.root=="mobile"){
bayeux.getClient().publish(POST.channelMobile,{text: POST.textMobile});
}
//eval(POST.auth_type+"_"+POST.update_type+"()");
}//end validate request
else
{
response.writeHead(404);
response.end('404 File not found');
}
});
response.end();
}
// Handle non-Bayeux requests
var server = http.createServer(function (request,response)
{
var pathRegex = new RegExp('^/publish/?$');
var pathname = url.parse(request.url).pathname;
if (pathRegex.test(pathname)) {
publish(request, response);
} else {
render404(request, response);
}
});
bayeux.attach(server);
server.listen(8000);
i use bayeux.getClient().publish(
to publish a message to a specific client .
i have created a subscription js
var client = new Faye.Client(n.node_url+':8000/faye/');
client.subscribe(n.channel, function(message) {
obj.processNotification(obj,message.text,n.user_id,n.user_type,n.channel);
});
the problem is , ihave no idea of how to create the channel
in
bayeux.getClient().publish(channel, message);
and how subscribe it , please help . thanks in advance ................
You don't create the channel, there is no prior setup to do, just publish to the channel and any listeners that are in that channel will receive the data.
You already have the code that subscribes to the channel in your code:
var client = new Faye.Client(n.node_url+':8000/faye/');
client.subscribe(n.channel, function(message) {
obj.processNotification(obj,message.text,n.user_id,n.user_type,n.channel);
});