title: Transfer msg with nodejs / socket.io to nodejs / express
body: <p>I'm having trouble to let my application runs as I would like... What I'm looking to do is to send a message from socket.io client (emitter.js) to socket.io server (receiver.js) and then display the message on the page generated using Express/EJS (index.ejs)</p>
  
  <p>What I've succeed to do so far is :</p>
  
  <pre><code>// receiver.js
  // This script initiates a simple HTML page using Express/EJS
  // and then wait for socket.io connection on port 3000
  const express = require('express')
  const app = express()
  const http = require('http')
  
  app.set('view engine', 'ejs')
  app.use(express.static('public'))
  
  app.get('/', function (req, res) {
      res.render('index.ejs', {params: 'some_params'})
  })
  
  app.server = http.createServer(app)
  
  var io = require('socket.io')(app.server)
  io.on('connection', function (socket) {
      socket.emit('init', 'Init OK !')
      console.log('init page ok')
      socket.on('new_data', function(msg) {
          console.log('socket.on received new data : ', msg)
          socket.emit('new_data', 'Test')
      })
  })
  
  app.server.listen(3000, function () {
    console.log('Main app listening on port 3000')
  })
  </code></pre>
  
  <p>And the attached index.ejs file :</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript" src="js/jquery-3.3.1.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div id='new_msg'&gt;&lt;/div&gt;
  
  
      &lt;script type="text/javascript"&gt;
          $(document).ready(function() {
          var socket = io()
          socket.on('init', function(msg) {
              console.log(msg)
              $('#new_msg').html(msg)
          })
          socket.on('new_data', function(msg) {
              console.log(msg)
              $('#new_msg').html(msg)
          })
      });
  &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Finally, the emitter.js script :</p>
  
  <pre><code>// emitter.js
  // Open io on port 3000 and then send a msg
  var io = require('socket.io-client')
  var socket = io.connect('http://localhost:3000')
  socket.on('connect', function() {
      socket.emit('new_data', 'some_new_data')
      console.log('Send some_new_data')
  })
  </code></pre>
  
  <p>I then run the server using <code>node receiver.js</code>
  The app starts, in the console I have the message : Main app listening on port 3000, and when I open a browser to <a href="http://localhost:3000" rel="nofollow noreferrer">http://localhost:3000</a>, I can see the first message Init OK!</p>
  
  <p>Thus, receiver.js can successfully emit to index.ejs !</p>
  
  <p>Then I run the client using <code>node emitter.js</code>. In the console of the emitter I have the message 'send some_new_data' and in the console of the receiver I have the message 'socket.on received new data : some_new_data'.</p>
  
  <p>Thus, emitter.js can successfully emit to receiver.js which can successfully receives it.</p>
  
  <p>But, where my problem is, is that the last line <code>socket.emit('new_data', 'Test')</code> is not executed (or not working) as the HTML page is not refreshed at all....</p>
  
  <p>So, I don't understand why the message is fired from receiver to index.ejs during initialisation but is not working after ??...</p>
  
  <p>If anybody would have some ideas regarding this problem, any help would be very much appreciate !</p>