I am working on a node project where I need to use websockets. I got the basic implementation working for socket.io in my project. Here is the code for server.js
so far
import app from '../app';
import http from 'http';
import socket from 'socket.io';
var server = http.createServer(app);
server.listen(app.get("port"));
server.on('error', onError);
server.on('listening', onListening);
const io = socket(server);
io.on('connection', (socket) => {
console.log(`connected ${socket.id}`);
socket.on('disconnect', () => {
console.log('disconnected');
});
});
Now if I need to call socket from another javascript file, how do I do that?
Something like this: From a.js
I would like to call emit message from download function. The problem is that I don't have reference to socket
object in a.js
. It was defined in my server.js
file. Any suggestions?
export const download = () => {
//after file is downloaded, emit message using socket.io to user
}