I am creating an application which in my eyes requires a variable that changes to be displayed in front of the client at any given time.
A given example, user foo has $100 dollars. An event happens which deducts $20 dollars from user foo's account. As soon as this deduction happens i would like the clients text to change to $80.
I have a fundamental understanding of how this needs to happen.
Here is the code I have,
app.js
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var balance = '100';
app.listen(80);
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.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://192.168.1.50');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
<html><p>Balance: balancehere.</p> </html>
</script>
I can imagine it would have to be emitted by app.js, but i have not the idea on how to display it to the client. You can see in the index.html, i have where i would like the variable to be displayed.