title: Send data from SerialPort to socket.io
body: <p>I'm struggling with socket.io, express, and node.js.</p>
  <p>I send data from an Arduino to my cmd. This is done with the serialport libary. But now I want this data displayed on my web browser. I'm using the express library for this. I have <strong><code>index.js</code></strong> here is the connection with the arduino and browser. And an index.html</p>
  <p>This code do I have:</p>
  <p>code from index.js (node.js server):</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require('express'),
      app = express(),
      server = require('http').Server(app),
      io = require('socket.io')(server),
      port = 8888;
  
  //Server start
  server.listen(port, () =&gt; console.log('on port' + port))
  
  //user server
  app.use(express.static(__dirname + '/public'));
  
  io.on('connection', onConnection);
  
  var connectedSocket = null;
  function onConnection(socket){
      connectedSocket = socket;
  }
  
  //Arduino to CMD
  const SerialPort = require('serialport');
  const Readline = SerialPort.parsers.Readline; 
  const usbport = new SerialPort('COM4');  
  const parser = usbport.pipe(new Readline()); 
  parser.on('data', console.log);</code></pre>
  </div>
  </div>
  </p>
  <p>The data what is recieved from the serialport (Arduino), had to be displayed in the index.html (webbrowser). I tried already something but it doesn't work. It has to be printed in the <code>&lt;p&gt;&lt;/p&gt;</code> in the html code.</p>
  <p>The <code>index.html</code> code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;div id="text"&gt;
      &lt;p&gt;&lt;/p&gt;
    &lt;/div&gt;
  
    &lt;script&gt;
      var text = document.getElementById('text');
  
      var socket = io.connect('http://localhost:8888');
  
      socket.on('data', function(message) {
        text.innerHTML = message.data;
      });
    &lt;/script&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>