Skip to content

Instantly share code, notes, and snippets.

@clopezpro
Forked from hansott/app.js
Created February 2, 2023 16:31
Show Gist options
  • Save clopezpro/7e9e12907a9ed0d7414a634d0641be83 to your computer and use it in GitHub Desktop.
Save clopezpro/7e9e12907a9ed0d7414a634d0641be83 to your computer and use it in GitHub Desktop.
Global socket.io in an application
var http = require('http');
var sockets = require('./sockets');
var server = http.createServer(app);
sockets.connect(server);
sockets.emit('event', { message: 'This is an event!' });
var socketIO = require('socket.io');
var io = null;
module.exports = {
connect: function(server) {
io = socketIO(server);
},
emit: function(event, values) {
if (io) {
io.sockets.emit(event, values);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment