Build your own TCP log server with node.js part 3

Eran Goldman-Malka · May 23, 2017

In the last part , we’ll push the logs and user comments to the database we created in part 1

A reminder, post schema can be generated by the command :

npm run docs

 

We’ll add simple handler on the user side in public/js/socketio.js:

var url      = window.location.href;
var socket = io.connect(url);

postURL = window.location.host;
postURL = "http://" + postURL.split(":")[0] + ":9000/logs";

socket.on('connect', function(data) {
  socket.emit('join', 'Hello from client');
});
socket.on('messages', function(data) {
  $("#future").empty().append(data);
});
$(document).ready(function() {
  var t = $('#dataTable').DataTable();

$("#addComment").closest('form').on('submit', function(e) {
    e.preventDefault();
    var data = {};
    data.source = 'user';
    data.type = 'comment';
    data.message = $("#future").text();
    data.comment = $("#CommentInput").val();

    $.post(postURL,data,function(data){console.log(data)});
    // $('#hiddenInput').val(someVariable); //perform some operations
    // this.submit(); //now submit the form
});

$.get(postURL,function(data){

  $.each(data, function( index, value ) {
  // $("#dataTable").append( index + ": " + value + "<br>");
  console.log(index);
    t.row.add([index,value['createdAt'],value['source'],value['type'],value['message'],value['comment']]);
  });
  t.draw();
});
});

and add datatables js and css to the public folder

It took around 20 minutes to build the base for a TCP log server…

next time we’ll handle the gui and add some more protocols and handlers to different type of messages

Twitter, Facebook