1

I am new to Node.js and i am developing a simple chat app using Socket.IO. I made an index.js file and an index.html file.

My index.js files looks like this

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
process.stdin.resume();
process.stdin.setEncoding('utf8');
var util = require('util');

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
     console.log('message: ' + msg);
   });
   var txt = process.stdin.on('data', function (text) {
     process.tick()
     return util.inspect(text)
   });
  socket.emit('chat message',txt);
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

My index.html file looks like this

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>

I am trying to take value from the console and print it on the browser and vice versa. In this app the chat is between the client and the server.

When i run the index.js file i get this on the terminal

listening on *:3000

All is fine until i hit the server with localhost:3000 from my browser and then i get this on the terminal

buffer.js:0
(function (exports, require, module, __filename, __dirname) { // Copyright Joy
^
RangeError: Maximum call stack size exceeded

How can i solve this? I want to get the value from the console(server) and send it to the browser(client).

Update 1

I get this error

process.tick()
             ^
TypeError: Object #<process> has no method 'tick'
    at ReadStream.<anonymous> (/home/sunil/Downloads/TPL/chat/index.js:16:14)
    at ReadStream.EventEmitter.emit (events.js:95:17)
    at ReadStream.<anonymous> (_stream_readable.js:746:14)
    at ReadStream.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at ReadStream.Readable.push (_stream_readable.js:127:10)
    at TTY.onread (net.js:526:21)

Update 2 process.tick() was replaced by process.nextTick() and then i get this error

node.js:415
            callback();
            ^
TypeError: undefined is not a function
    at process._tickCallback (node.js:415:13)

1 Answer 1

2

RangeError: Maximum call stack size exceeded. The error is generated by infinite loop somewhere in your app. Updated code inside io.on('connection' ...

var txt = '';
process.stdin.on('data', function (text) {
     process.nextTick();
     txt = util.inspect(text);
});
io.on('connection', function(socket){
      socket.on('chat message', function(msg){
         console.log('message: ' + msg);

         socket.emit('chat message',txt);
       });
    });

Putting socket.emit inside io.on('connection', function(socket){}); directly will be continuously emitting that socket event' like a infinite loop and raises stack size range error. So, Please update your code as above.

Sign up to request clarification or add additional context in comments.

5 Comments

I updated the code with your code and I got the message from client to server but with this in addition to message - Missing error handler on socket. undefined - I understood that i need to do error handling. Another thing that i am still not able to send message from server to client. When i type the message on server and hit enter, i get this the error.. see the updated question for the error.
Ok another update. The function process.tick() is wrong and the correct function is process.nextTick(). I corrected the function and then i got this error message. node.js:415 callback(); ^ TypeError: undefined is not a function at process._tickCallback (node.js:415:13)
As in completely outside the io.on() or outside the socket.on() but inside io.on()
Its not working. Still the same error. node.js:415 callback(); ^ TypeError: undefined is not a function at process._tickCallback (node.js:415:13)
function doesnot return value however you can start a callback or set the desired value to another variable without any callback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.