Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node-irc with express/socket.io #417

Closed
ghost opened this issue Oct 19, 2015 · 7 comments
Closed

node-irc with express/socket.io #417

ghost opened this issue Oct 19, 2015 · 7 comments

Comments

@ghost
Copy link

ghost commented Oct 19, 2015

I'm building a web chat using node-irc.

I'm new to express/node.js/socket.io in general so please excuse any bad mistakes, but I'm following the documentation on the socket.io website as well as the node-irc documentation on here and Read The Docs.

index.js:

io.on('connection', function(socket){
  var client = new irc.Client('localhost', 'myNick', {
      channels: ['#webchat'],
  });

  client.addListener('message', function (from,to,message) {
    io.emit('message', message);
  });

  socket.on('disconnect', function(){
  });
});

index.html:

    <script>
      var socket = io();

      socket.on('message', function(message){
        $('#messages').append('<p>' +  message + '</p>');
      });
    </script>

But in #messages I see each message 3 times, why is this?

3�hi ya���13�hi ya���13�hi ya���03�Welcome Dimitra ���03�Welcome Dimitra ���03�Welcome Dimitra

@ghost
Copy link

ghost commented Oct 19, 2015

This is because every time a webclient connects to the websocket, its re-initializing a brand new node-irc bot, including the 'addListener'. You may not see see the other two bots join, because they all have the same nick, but only left with the symptom of three messages 1/per each bot. You probably just loaded the page to your web application and then refreshed twice. If you can, I'd recommend putting the client object as a global instead of inside the websocket initialization.

@ghost
Copy link
Author

ghost commented Oct 20, 2015

@jnull I want each user who connects to have their own connection so for example 100 users can open the page and it creates 100 connections. I guess I'm understanding it wrong. Can you point me in the right direction?

@ghost
Copy link

ghost commented Oct 20, 2015

you probably want something jerry-rigged like this:

var user_id = 0; 
io.on('connection', function(socket){
user_id++; 
  var client = new irc.Client('localhost', 'myNick' + user_id, {
      channels: ['#webchat'],
  });

  client.addListener('message', function (from,to,message) {
    io.emit('message', message);
  });

  socket.on('disconnect', function(){
  });
});

If that doesn't work, you may have to get jiggy with the client object and push ever new subsequent client to an array like:

var client_list_array = [];
var client_list_array.push({new irc.Client: etc...});

@ghost
Copy link
Author

ghost commented Oct 20, 2015

I fixed it by changing io.emit('message', message); to socket.emit('message', message);, so the message is sent to that socket only, not all.

@ghost
Copy link

ghost commented Oct 20, 2015

oh i thought it was on your ircd #messages channel, not your web application, yep that would be it then.

@ghost
Copy link
Author

ghost commented Oct 20, 2015

@jnull how can I convert messages to CSS colours?

pervena2: �10,00wb Okwaho� is an example message, how do I get the 10,00 converted to CSS colours?

@ghost
Copy link

ghost commented Oct 20, 2015

CSS is out of scope to node-irc, you will need to go elsewhere to find an html/css tutorial. If your original problem has been solved, please close this and create a new issue, but only if its a node-irc issue. Thanks!

@ghost ghost closed this as completed Oct 21, 2015
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants