Socket.io emit on Express Route
<p>I want to emit some data to the client when some API route gets called.
I have to following code on server.js</p>

<pre><code>var app  = express();
var http = require('http').Server(app);

var io   = require('socket.io')(http);

io.on('connection', function(socket){
  console.log('a user connected');

  socket.emit('tx', 'msg');

  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
});
</code></pre>

<p>Now I have this route /test:</p>

<pre><code>var bsg   = require('./routes/test');
</code></pre>

<p>And that file:</p>

<pre><code>var express      = require('express');
var passport     = require('passport');
var router       = express.Router();

router.get('/test',  function(req, res) {
  //work here
});

module.exports = router;
</code></pre>

<p>On client side:</p>

<pre><code>&lt;script type="text/javascript"&gt;
   var socket = io();

   socket.on('tx', function(data) {
     console.log(data);
   });
&lt;/script&gt;
</code></pre>

<p>Whats the best solution for this?</p>

<p>Thanks!</p>

<p>Express 4 / socket.io 1.4.5</p>