Is it possible to run some script and if client connected pass arguments to it, something like this:
var io = require('socket.io').listen(http);
io.sockets.on('connection', function (client) {
console.log('Client connected');
var notifikacija = function (array) {
client.emit('populate', array);
}
});
///////////////////////////////////////////////////////////////////////
setInterval(function(){
var array = newArray();
array[0]='test';
notifikacija(array);
}, 2000);
Now it shows error: notifikacija is not defined. It is quite a strugle...
The notifikacija
function is local to the scope of the io.sockets.on handler. You want it to be global so that you can access it in setInterval:
var notifikacija = function(){}; // just an empty function, in case it gets called before it has something to do
var io = require('socket.io').listen(http);
io.sockets.on('connection', function(client) {
console.log('Client connected');
notifikacija = function(array){ // once the client is available assign the function
client.emit('populate', array);
}
});
setInterval(function(){
var array = newArray();
array[0]='test';
notifikacija(array);
}, 2000);
Here's a blog post with some more information on scope in Javascript.