How to create a loop to send an event in socket.io?

Asked

Viewed 390 times

7

I have an architectural problem.

I have a code using socket. in nodejs:

socket.on('images',function (aData){
    ...
    socket.sockets.emit('show', JSON.stringify({imagens : json}))
});

I’m trying to broadcast time in time at the "show" event, but the connected sockets can not keep sending the event images all the time.

A loop in the backend itself could solve the issue, but I need a trigger for the event images other than connected sockets.

Any solution or alternative?

1 answer

4


Maybe the setInterval() (English) help you, by using an anonymous function:

var show = setInterval(function() {
    socket.sockets.emit('show', JSON.stringify({imagens : json}))
}, 10000);

The second argument concerns the time in milliseconds that the function setInterval() should wait between each call.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.