7

i m creating chat application, using nodejs (0.8.15), express (>3.0) framework and mongodb for register users.

var express = require('express') , http = require('http') , path = require('path') , io = require('socket.io'); var app = express() , server = http.createServer(app) , io = io.listen(server); app.configure(function() { app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser('secret')); app.use(express.session({cookie: {maxAge: 60000*100}})); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); }); app.configure('development', function() { app.use(express.errorHandler()); }); app.get('/chat', function(req, res) { res.render('chat'); }); server.listen(app.get('port'), function() { console.log("Express server listening on port " + app.get('port')); }); io.sockets.on('connection', function (socket) { socket.on('start-chat', function() { // here i need to know req and res // for example, i need to write: // socket.username = req.session.username; }); }); 

Q: How to get res and req objects to work with them when chatting like on code above? or am i going wrong way of creating chat with user auth?

thank you!

EDITED: answer is here http://www.danielbaulig.de/socket-ioexpress/

3 Answers 3

7

You need to use authorization.

var socketIO = require('socket.io').listen(port); socketIO.set('authorization', function(handshakeData, cb) { //use handshakeData to authorize this connection //Node.js style "cb". ie: if auth is not successful, then cb('Not Successful'); //else cb(null, true); //2nd param "true" matters, i guess!! }); socketIO.on('connection', function (socket) { //do your usual stuff here }); 
Sign up to request clarification or add additional context in comments.

Comments

6

You cannot get res and req objects in a socket.io handler, as they simply do not exist - socket.io is not normal http.

Instead, what you can do is authenticate users and assign them a session auth token (a key that identifies that they're logged in and who they are). Then the client can send the auth token along with every socket.io message, and the server-side handler can just check the validity of the key in the database:

io.sockets.on('connection', function (socket) { socket.on('start-chat', function(message) { if (message.auth_token) //Verify the auth_token with the database of your choice here! else //Return an error message "Not Authenticated" }); 

Comments

-2

on socket.io v1.0 and above you can get the req object like this

var req = socket.request; var res = req.res; 

1 Comment

This is wrong. This is not the same request that is being asked in question. You will not find your request parameter in above socket.request.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.