I am trying to configure my server using websockets so that when I update something using my routes, I can also issue a websocket message when something on this route is updated.
The idea is to save something on my mongo-db when someone gets on the /add-team-member route, for example, then gives a message to everyone who is connected via websocket, and is part of any room on the Internet, which corresponds to this command.
I followed the documentation for socket.io to configure my application as follows:
App.js
// there a lot of code in here which sets what to use on my app but here the important lines const app = express(); const routes = require('./routes/index'); const sessionObj = { secret: process.env.SECRET, key: process.env.KEY, resave: false, saveUninitialized: false, store: new MongoStore({ mongooseConnection: mongoose.connection }), secret : 'test', cookie:{_expires : Number(process.env.COOKIETIME)}, // time im ms } app.use(session(sessionObj)); app.use(passport.initialize()); app.use(passport.session()); module.exports = {app,sessionObj};
start.js
const mongoose = require('mongoose'); const passportSocketIo = require("passport.socketio"); const cookieParser = require('cookie-parser'); // import environmental variables from our variables.env file require('dotenv').config({ path: 'variables.env' }); // Connect to our Database and handle an bad connections mongoose.connect(process.env.DATABASE); // import mongo db models require('./models/user'); require('./models/team'); // Start our app! const app = require('./app'); app.app.set('port', process.env.PORT || 7777); const server = app.app.listen(app.app.get('port'), () => { console.log(`Express running β PORT ${server.address().port}`); }); const io = require('socket.io')(server); io.set('authorization', passportSocketIo.authorize({ cookieParser: cookieParser, key: app.sessionObj.key, // the name of the cookie where express/connect stores its session_id secret: app.sessionObj.secret, // the session_secret to parse the cookie store: app.sessionObj.store, // we NEED to use a sessionstore. no memorystore please success: onAuthorizeSuccess, // *optional* callback on success - read more below fail: onAuthorizeFail, // *optional* callback on fail/error - read more below })); function onAuthorizeSuccess(data, accept){} function onAuthorizeFail(data, message, error, accept){} io.on('connection', function(client) { client.on('join', function(data) { client.emit('messages',"server socket response!!"); }); client.on('getmessage', function(data) { client.emit('messages',data); }); });
My problem is that I have many changes to the mongo save file that happen in my ./routes/index file, and I would like to be able to send messages from my routes and not from the end of start.js where the socket is .io.
Is it possible to somehow transfer the websocket message from my ./routes/index file, even if IO is configured further down the line in start.js?
for example, something like this:
router.get('/add-team-member', (req, res) => { // some io.emit action here });
Perhaps I need to move to where I initialize the socket.io file, but could not find any documentation about this, or maybe I can access socket.io from the routes already somehow?
Thanks and appreciate the help, let me know if something is unclear!