1

I have two files -

  • index.html
  • index.js

In index.html, I have a text box. I want to display the message typed on the text box on to the console (the command prompt of node.js).

enter image description here

In index.html, I wrote this -

<script>
  var socket = io();
  $('form').submit(function(){
    socket.emit('chat message', $('#m').val());
    $('#m').val('');
    return false;
  });
</script>

On server side, in index.js I used this -

socket.on('chat message', function(msg){ //here is the listener to the event
    console.log('message: ' + msg);
  });

But, the message is not getting displayed in the console. What's wrong with my code? Please refer the below files, if needed.

index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

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

io.on('connection', function(socket){
  console.log('a user connected');

  socket.on('disconnect', function(){
    console.log('user disconnected');
  });

socket.on('chat message', function(msg){ //here is the listener to the event
    console.log('message: ' + msg);
  });

});


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

index.html

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
   //some code
    </style>
  </head>

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

  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>
8
  • do you see anything printed on console? Commented Jul 26, 2015 at 3:58
  • Which console are you looking at, the browser console or the command line console? Commented Jul 26, 2015 at 3:59
  • 1
    Try moving the <script> block in your html file that contains the socket.io code before the closing </body> tag. Commented Jul 26, 2015 at 4:05
  • 1
    @BidhanA Perfect! Thanks Bidhan, it worked. I think the body isn't loaded by the time the script is loaded. As you suggested, moving it after the <body> worked. Thanks! Commented Jul 26, 2015 at 4:10
  • 1
    Please write the same as an answer, so that I can accept it! :) Commented Jul 26, 2015 at 4:11

1 Answer 1

3

Move the <script> block in your html file that contains the socket.io code before the closing </body> tag so that the script is loaded only after the body completes loading.

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

Comments

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.