how to to create the path of the view to subscribe ,to a message published by bayeux.getClient().publish(

298 views Asked by At

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 ................

2

There are 2 answers

0
Rishikesh Chandra On

Basically on server side you can create a logic for creating distinct channels and save it in your DB for your clients to subscribe on it and use the same for communication.

For example There could be two users, A and B. The time users A and B onboard on your server, that time you can make two channels for each, based on the combination of their user id's and name and some dynamic number. This gives all the users their default channels to listen and subscribe. These channels can be used for sending messages from server to the clients, which can act as notifications to the client.

Now for communication purpose, there can be a channel such as OpenTalks on which all the users are subscribed, which can be used for chatting.

You can refine the making of channels more for one to one conversation.

bayeux.getClient().subscribe('/'+channel_name, message);
bayeux.getClient().publish('/'+channel_name, message);

Or you can use

const faye = require('faye');
var client = new faye.Client('http://localhost:3000/faye',{timeout: 20});
client.connect();
client.subscribe('/'channel_name, function(message){console.log(message);});
client.publish ('/'+response1[0].channel_id, {channel_name: channel_name,message: message});
0
Pablo Fernandez heelhook On

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