i finally found something and with a little tweak and researching other answers i have finally made a working code
First a brief review of what this code does
watches your json file (in this case sports.json)
if change detected then only reads the json file (in this case sports.json)
then emits the read json file to connected clients
on the client side the magic begins as soon as you make changes to your json file
PS: There is a discussion regarding fs.watch firing twice on manual editing and saving (i will come up with a workaround soon and update here)
Server Side
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var jf = require('jsonfile'); //jsonfile module
var fs = require('fs'); //require file system
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket) {
fs.watch("sports.json", function(event, fileName) { //watching my sports.json file for any changes
//NOTE: fs.watch returns event twice on detecting change due to reason that editors fire 2 events --- there are workarounds for this on stackoverflow
jf.readFile('sports.json', function(err, data) { //if change detected read the sports.json
var data = data; //store in a var
console.log('sent') //just for debugging
socket.volatile.emit('notification', data); //emit to all clients
});
});
});
http.listen(3000, function() { //listen to 3000
console.log('listening on *:3000');
});
Client Side:
<!doctype html>
<html>
<head>
<title>Socket.IO data</title>
<body>
<p id ="data">A data will appear here on change</p>
<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.connect('http://localhost:3000'); //you can replace localhost with your public domain name too!!
socket.on('notification', function (data) {
$('#data').text(data.football.home); //Liverpool
});
</script>
</body>
sports.json File
{
"football": {
"id": 1,
"home": "Liverpool",
"away": "Chelsea",
"score": "1-0",
"last scorer":"Gerrard"
}
}