My node.js socket.io chat app is not emitting the chat message to other users (when i open another browser). The message from browser A would only show up in browser A.
server.js
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs');
app.listen(4000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.on('sendChat', function (data) {
// console.log('sendChat', data);
socket.emit('displayChat', data);
});
});
index.html
<html>
<head>
<title>Chat</title>
<style type="text/css">
#chats {
overflow: auto;
}
#sender {
position: absolute;
bottom: 0;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function(){
var socket = io.connect('http://localhost');
var chats = $("#chats ul");
socket.on('displayChat', function (data) {
// console.log("displayChat", data);
chats.append($("<li></li>").text(data));
});
var author = $("#author");
var message = $("#message");
message.keypress(function(event) {
if ((event.keyCode || event.which) == 13 && author.val() != '') {
// console.log("sendchat", author.val() + ": " + message.val());
socket.emit('sendChat', author.val() + ": " + message.val());
message.val('');
}
});
});
</script>
</head>
<body>
<div id="chats">
<ul></ul>
</div>
<div id="sender">
<input id="author" type="text"><input id="message" type="text">
</div>
</body>
</html>
here is a log
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized T0NOrPaqIkQMXOJjgz9E
debug - setting request GET /socket.io/1/websocket/T0NOrPaqIkQMXOJjgz9E
debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
debug - client authorized for
debug - websocket writing 1::
sendchat a: hi
debug - websocket writing 5:::{"name":"displayChat","args":["a: hi"]}
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized PL2VRwE2V0UvBc6dgz9F
debug - setting request GET /socket.io/1/websocket/PL2VRwE2V0UvBc6dgz9F
debug - set heartbeat interval for client PL2VRwE2V0UvBc6dgz9F
debug - client authorized for
debug - websocket writing 1::
sendchat b: hi
debug - websocket writing 5:::{"name":"displayChat","args":["b: hi"]}
info - transport end (socket end)
debug - set close timeout for client PL2VRwE2V0UvBc6dgz9F
debug - cleared close timeout for client PL2VRwE2V0UvBc6dgz9F
debug - cleared heartbeat interval for client PL2VRwE2V0UvBc6dgz9F
debug - discarding transport
debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
debug - websocket writing 2::
debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - got heartbeat packet
debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized I3S_VVYyencv9VQOgz9G
debug - setting request GET /socket.io/1/websocket/I3S_VVYyencv9VQOgz9G
debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
debug - client authorized for
debug - websocket writing 1::
sendchat b: hi
debug - websocket writing 5:::{"name":"displayChat","args":["b: hi"]}
debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
debug - websocket writing 2::
debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - got heartbeat packet
debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
debug - websocket writing 2::
debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
debug - got heartbeat packet
debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
debug - websocket writing 2::
debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - got heartbeat packet
debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
debug - websocket writing 2::
debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
debug - got heartbeat packet
debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
sendchat a: digg
debug - websocket writing 5:::{"name":"displayChat","args":["a: digg"]}
debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
debug - websocket writing 2::
debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - got heartbeat packet
debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
debug - websocket writing 2::
debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
debug - got heartbeat packet
debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
sendchat b: dd
debug - websocket writing 5:::{"name":"displayChat","args":["b: dd"]}
debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
debug - websocket writing 2::
debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - got heartbeat packet
debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
debug - websocket writing 2::
With socket you are emiting only to the current connection. You need to emit using io.sockets and it will be emitted to any open socket.
Change this:
io.sockets.on('connection', function (socket) {
socket.on('sendChat', function (data) {
// console.log('sendChat', data);
socket.emit('displayChat', data);
});
});
To this:
io.sockets.on('connection', function (socket) {
socket.on('sendChat', function (data) {
// console.log('sendChat', data);
io.sockets.emit('displayChat', data);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With