I need to emit data only to the user that request it. So far I get:
app.js file (server):
var express = require('express');
var app = express();
app.set('view engine', 'ejs');
app.use(function(req, res, next){
req.io = io;
next();
});
var mainPath = require('./routes/mainPath.js');
app.use('/', mainpath);
port = 8000;
var io = require('socket.io').listen(app.listen(port));
io.on('connection', function(socket){
console.log('connected');
});
mainpath.js file:
var express = require('express');
var app = express();
var router = express.Router();
var request = require('request');
router.post('/path', function(req, res){
request('http://google.com', function (err, resp, content_body){
req.io.emit('socketio emit', {socketioEmit : body});
});
});
// [!]
module.exports = router;
[!] problem appear when... request used here for example (or any fast executing data), grab data faster then socket.io connection is established, and the user don't receive data.
io.js file (client):
var socket = io();
socket.on('connect', function(){
console.log('connected');
});
socket.on('socketio emit', function(emit){
console.log(emit.socketioEmit);
});
mainpath.ejs file:
<html>
<head></head>
<body>
<div class="form_seo_wrapper">
<form action="/path" method="post">
<input type="text" name="input_name" />
<input type="submit" value="go" />
</form>
</div>
</body>
</html>
path.ejs file:
<html>
<head></head>
<script src="socket.io.min_v1.7.2.js"></script>
<body>
<h1>Output:</h1>
</body>
<script src="io.js"></script>
</html>
Any ideas? BTW is there any possibility to not break socket connection when user enter from '/' to '/path' ?