Is there a way to emit to the current socket within a post method of node.js using socket.io and express without having to go through the io.sockets.on('connection')?
Here is my issue. I am making a mini authorization system. When the user submits from the form it post the data through '/login' instead of using a onclick emit. If the user has an invalid user-name or password it should send a message back notifying it failed. I would prefer not to use the callback or write because I am using a Jade template. You can see my source code here.
Example: Server Side
var LoggIn = require('./lib/login.js'); // <-- Middle-ware app.post('/login', function (req, res){ LoggIn.authenticate(req.body.user, req.body.password, function(user, msg) { if (user) { req.session.user = user; res.redirect('/'); } else { res.render('login'); //res.emit('loginFail', 'You have entered an invalid user / password!<br>Try Again.); // <-- wishing for something like this console.log('Login Failed! : ' + msg); } }); }); Example Client Side
JS
var status = document.getElementById('status'); socket.on('loginFail', function(msg) { status.innerHTML = msg; }); Jade Form
#status form#loginForm(method='POST', action='/login') input(name='user', placeholder='User Name') br input(name='password', placeholder='Password', type='password') br input(value='Sign In', type='submit')
socket.emit('loginFail','Can't log in');Also I don't know if you can use sockets for this as the client would still need to connect after rendering the page, in which they would miss the emit. Is there a reason you can't do something like thisres.render('login',{error:'Invalid credentials'});and then display the status if it exists