0
votes

I'm quite new on node.js and now I'm learning socket.io. I'm developing an app step by step so, at this time, I have an app that can login an user, do crud operation in mysql and mongodb and upload files, all these operations are manage with some web pages with HTML and javascript technologies launched directly from restify. After that I'm tring to add socket functionality to, at this time, simple print who is online. So, before I have something like:

server.get('/login', function(req, res, next){ ... });

and now I have something like:

socket.on("login", function (req, res, next){ ... });

but, naturally, req and res are undefined! Are there the same objects into socket.io?

1
Sorry my friend but you'll need to elaborate more your question because it's unclear.iniestar
@iniestar : is better now?Filippo1980

1 Answers

0
votes

To my understanding, you want to pass values back and forth in your request and response using socket.io.

Yes it is possible to do that and you syntax should be something like this...

Using express.js:

io.on('login', function(req){
client.emit('response event', { some: 'data' });

Note: when using emit you send the data to everyone, you have other methods like .broadcast(), .to(), etc.. for other use cases refer to socket.io github for better understanding And lastly, inside emit you define the function you want to call on the client side and the data you want to send to the client.