How would I access socket in the global scope based on my following NodeJS code
io.on('connection', function (socket) {
console.log('connection '+socket)
socket.on("data",function(d){console.log('data from flash: ',d);});
socket.emit("message","wtfwtwftwftwf hello from server");
socket.on('disconnect', function (socket) {
console.log("disconnect");
});
});
I need to access socket from within the following app.post method
var express = require('express'),
multer = require('multer');
var app = express();
//auto save file to uploads folder
app.use(multer({ dest: './uploads/'}))
app.post('/', function (req, res) {
console.log(req.body); //contains the variables
console.log(req.files); //contains the file references
res.send('Thank you for uploading!');
});
app.listen(8080);
Haven't tested yet but going to try a simple getter function first
io.on('connection', function (socket) {
console.log('connection '+socket)
socket.on("data",function(d){console.log('data from flash: ',d);});
socket.emit("message","wtfwtwftwftwf hello from server");
return{
getSocket: function(){
return socket;
}
};
socket.on('disconnect', function (socket) {
console.log("disconnect");
});
});
io.getSocket() ??
Express's
app
and Socket.io have nothing to do with one another.So fundamentally, you can't use
socket
insideapp.post
.You need to identify the client. You can use Passport which has a Socket.io plugin that essentially bridges the
app.post
/get
'sreq.user
tosocket.request.user
.Note: It doesn't have to be an authenticated client with user that's fetched from database, just a client with a temporary user stored in memory would do. Something like this:
Then you can attach the client's socket ID to its user session.
And then instead of
socket.emit
useio.emit
inapp.post
using thesocketid
Note:
io.to()
is used to emit to a room, but every socket is by default joined to the same room named as itssocket.id
, so it'll work.Javascript and socketIO experts > please tell me why this simple solution shouldn't work. It seems to...
1 Define a global pointer
2 In my socketIO handler make a reference to the socket object
3 And finally within app.post, access the socket like thus