tail a log and send out using socket.io

316 views
Skip to first unread message

Angelo Chen

unread,
Nov 10, 2012, 3:03:48 AM11/10/12
to nodejs
Hi,

I was trying to tail a log and send the out put to a connected
browser, i can see the log data in the part of:
tail_child.stdout.on('data', function (data) from console.log, but
the socket.broadcast.emit seems not sending out any data, any way to
make this work? thanks, Angelo

part of app.js

socketio.listen(server).on('connection', function (socket) {
console.log("new socket")

// everytime there is a new request, spawn an child process
var tail_child = spawn('tail', ['-f', '/var/log/system.log']);

tail_child.stdout.on('data', function (data) {
// write it on the console
console.log(data.toString()); // yes, i can see the data here
// send it to the browser because the browser will be waiting
for the next chunk of data
socket.broadcast.emit('message', data.toString()); // no,
browser can not see any
});

socket.on('disconnect', function () {
tail_child.kill();
console.log('disconnected me')
});
});
index.html

<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/
jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(function () {
var iosocket = io.connect();

iosocket.on('connect', function () {
$('#incomingChatMessages').append($
('<li>Connected</li>'));

iosocket.on('message', function (message) {
$('#incomingChatMessages').append($('<li></
li>').text(message));
});
iosocket.on('disconnect', function () {
$
('#incomingChatMessages').append('<li>Disconnected</li>');
});
});

$('#outgoingChatMessage').keypress(function (event) {
if (event.which == 13) {
event.preventDefault();
iosocket.send($
('#outgoingChatMessage').val());
$('#incomingChatMessages').append($('<li></
li>').text($('#outgoingChatMessage').val()));
$('#outgoingChatMessage').val('');
}
});
});
</script>
</head>

<body>Incoming Chat:&nbsp;
<ul id="incomingChatMessages"></ul>
<br />
<input type="text" id="outgoingChatMessage">
</body>

</html>

Cryptic Swarm

unread,
Nov 10, 2012, 4:14:17 AM11/10/12
to nod...@googlegroups.com
You are starting a new process for every connection.
You are using the broadcast flag in socket.io.

Broadcasting means sending a message to everyone else except for the socket that starts it.
"""

In your case you would likely have the messages show up N-1 times (where N is the number of clients connected).  If you only had one client connect you would see the messages 0 times.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Angelo Chen

unread,
Nov 10, 2012, 4:53:26 AM11/10/12
to nod...@googlegroups.com
you are correct, removing broadcast works. a not so related question, in the browser side, I'd like to display only 10 of the lines, if more than that, it scrolls up, what's a good approach to this? thanks.

Alex Kocharin

unread,
Nov 10, 2012, 5:19:27 AM11/10/12
to nod...@googlegroups.com
Angelo,
 
see pupergrep ( https://github.com/bobrik/pupergrep ), I think it's does exactly the same as an app you are trying to implement.
--
// alex
 
 
10.11.2012, 13:53, "Angelo Chen" <angelo...@gmail.com>:

Angelo Chen

unread,
Nov 10, 2012, 7:26:27 AM11/10/12
to nodejs

thanks for the link, that pupergrep gives me some ideas about what I'm
trying to do.
Reply all
Reply to author
Forward
0 new messages