2

I am connecting to an existing irc server from my site. I have a couple of listeners that keep logging the data to the console. My problem is how to pass this data to the ejs file without rendering the file again and again.

irc.addListener('kick', function (channel, who, by, reason) {
                console.log('%s was kicked from %s by %s: %s', who, channel, by, reason);
            });

This is the code that keeps logging the data to the console. I need to send the variables to the view whenever there is some event

2 Answers2

1

The most straightforward way to do this is with websockets. Have a look at socket.io, they have a number of examples including chat clients.

/*Edited to add */ I would strongly recommend you do a few of the Socket.io tutorials on your own so that you get a good understanding of what's happening and why.

That said, to get you started, the following code should be a really basic starting point:

var io = require('socket.io')(80); // or whatever port you're using
var irc = require('./path/to/your/irc/client.js');
var format = require('util').format;

// not sure why yours uses 'addListener' syntax, but if you control it, I'd favor node semantics
irc.on('kick', function(channel, who, by, reason){
  io.emit('message', format('<%s> %s', from, message));
});

The additional code you had with the socket.io code listening for connections, etc, is not strictly necessary for what you described your needs being, but could be added if you want the client to be able to use the socket to send stuff to the server.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • bot.addListener('message', function (from, message) { console.log('<%s> %s', from, message); /* console.log("entered function"); console.log("function exited");*/ }); io.on('connection', function(socket){ socket.on('chat message', function(){ io.emit('chat message', message); }); }); How Should I combine the two function, I need to pass the message variable to the io.on function? –  Apr 03 '16 at 14:24
0

There are two ways:

JSON: Create a JSON file and a string variable, then append every irc action to the string (var += ircstring) and create a Timer that always writes this string to the JSON File, then use jQuery and DOM to update the content on the Website (document.getElementById(...).innerHTML = json output) Thats one way, but I don't think that this way is resource friendly.

The better way would be: socket.io This is an npm module that lets you interact with the client continuosly. The Node.js app interacts just like a Socket Server, and the Webbrowser (or more than one Webbrowser) acts like a Client Socket. Just have a look at their website, they have a good Tutorial for building a Chat with socket.io and express!

Yassine Zeriouh
  • 480
  • 3
  • 10
  • 23
  • bot.addListener('message', function (from, message) { console.log('<%s> %s', from, message); /* console.log("entered function"); console.log("function exited");*/ }); io.on('connection', function(socket){ socket.on('chat message', function(){ io.emit('chat message', message); }); }); How Should I combine the two function, I need to pass the message variable to the io.on function? –  Apr 03 '16 at 14:22