Skip to content

Instantly share code, notes, and snippets.

@iamnasirudeen
Created May 16, 2019 08:58
Show Gist options
  • Save iamnasirudeen/55a5f49ef66c66655545c899ac3ee628 to your computer and use it in GitHub Desktop.
Save iamnasirudeen/55a5f49ef66c66655545c899ac3ee628 to your computer and use it in GitHub Desktop.
module.exports = function(io){
const express = require('express');
const router = express.Router();
router.get('/', (req, res, next) => {
io.emit('welcome', 'Welcome to our website)
)
}
// So the question is i can emit using io (but io will make it global, i.e all connected sockets will receive this message, but i only want the new ly connected socket to receive the message)
so i must use socket.emit(according to socket.io docs).. buh how can i use socket here when am only passing io.. though if i try using socket in server.js.. after have listened for connection , it will work
but i want to extend it to other routes
const http = require('http')
const express = require('express')
const app = express();
const server = http.createServer(app).listen(process.env.PORT || 3000, () => {
console.log(`Server listening on port: ${port}`)
})
const indexRouter = require('./routes/index');
const socketIo = require('socket.io)(server)
socketIo.on('connection', socket => {
console.log('A user has connected');
socket.on('disconnect', client => {
console.log('A client has disconnected');
});
});
app.use(indexRouter(socketIo));
module.exports = app;
const socket = io()
socket.on('welcome', {
document.write(data)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment