Skip to content

Instantly share code, notes, and snippets.

@danfinlay
Last active December 14, 2015 10:39
Show Gist options
  • Save danfinlay/5073527 to your computer and use it in GitHub Desktop.
Save danfinlay/5073527 to your computer and use it in GitHub Desktop.
A question regarding socket.io
//I need to be handling an occasional data influx whether or not a client is connected,
//but if one is connected, I'd like them to be notified of it.
//The problem is I don't know how to do that with socket.io
//A simple example of how I wish I could write it would be:
//
occasionallyGetData(function(dataGot){
socket.emit('newData', dataGot)
regularlyHandle(dataGot)
})
io.sockets.on('connection', function (socket) {
});
//Could I assign the socket to a global object? I suspect that would only support one connection:
var globalSocket
occasionallyGetData(function(dataGot){
if(globalSocket)
globalSocket('newData', dataGot)
regularlyHandle(dataGot)
})
io.sockets.on('connection', function (socket) {
globalSocket = socket
});
//What's a way?
@danfinlay
Copy link
Author

I updated the example to clarify my situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment