Real time feed update using is platform independent web framework which allows us to program real time bidrectional communication betweeen various devices. Using we can build some real time application such as Live chat OR real time analytics of shares etc.

I have already covered the “event emitter” of Node.js and in this tutorial i am going to focus on covering with simple application.

How it works ? is available for Node.js and also available as minified JS for client side integration so not only from Server you can emit and recieve events from Client side as well.


There are some common and most used function which you need to understand before moving ahead.

  • .on : This is listener.
  • .emit : This invokes and trigger the event.

For now we are using these basic function but there are more to learn ! So once the “invoker” triggers the event with some event identification particular listener or if its broadcast then every listener will catch those events and retrieve information from them.

Our application:

I am going to build simple status box which allows user to add the status and in real time update it to feeds of everyone. This is just to show how we can integrate in real time projects.

Database :

Create MySQL database name as Status and create table using following Query :

CREATE TABLE `fbstatus`
    `s_text` TEXT,
     PRIMARY KEY ( `status_id` )

Here is our package.json.

  "name"          :     "facebook-chat-socket",
  "version"       :     "0.0.1",
  "dependencies"  : {
      "express"   : "~4.11.2",
      "" : "~1.3.3",
       "mysql"    : "~2.5.5"

Install dependencies by typing

npm install

Here is main Server file.

var app       =     require("express")();
var mysql     =     require("mysql");
var http      =     require('http').Server(app);
var io        =     require("")(http);

/* Creating POOL MySQL connection.*/

var pool    =    mysql.createPool({
      connectionLimit   :   100,
      host              :   'localhost',
      user              :   'root',
      password          :   '',
      database          :   'fbstatus',
      debug             :   false

    res.sendFile(__dirname + '/index.html');

/*  This is auto initiated event when Client connects to Your Machien.  */

    console.log("A user is connected");
    socket.on('status added',function(status){
            io.emit('refresh feed',status);
        } else {

var add_status = function (status,callback) {
        if (err) {
    connection.query("INSERT INTO `status` (`s_text`) VALUES ('"+status+"')",function(err,rows){
            if(!err) {
     connection.on('error', function(err) {

    console.log("Listening on 3000");

Here is our client side code.

    <script src="/"></script>
    <script src=""></script>
    <link rel="stylesheet" href="//">
    <script src = ""></script>
          var socket = io();
            socket.emit('status added',$("#comment").val());
          socket.on('refresh feed',function(msg){
            $("#show_comments").append(msg + '<br /><br />');
    <div id="comment_box" style = "padding:5%;">
      <textarea id="comment" rows="5" cols="70"></textarea><br /><br />
      <input type="button" id="add_status" value="Add Status">
       id    = "show_comments"
       class = "jumbotron"
       style = "width: 38%;
                height: 100%;
                padding: 2%;


Output :

Run the program using

node Server.js

Visit “http://localhost:3000” to view the app. Open up one more window and see the updates on each window automatically.
Check the database, you can see the entries there !
databse entries

Conclusion :

You can develop really cool apps using I am not sure though but i think Facebook comments and Twitter home page which updates the tweets as soon as someone tweet is some how related or integrated the Let me know if you have already built any app using this !

52 thoughts on “Real time feed update using”

  1. “Create MySQL database name as Status and create table using following Query”. your database name is “fbstatus” not “status”.

  2. Also in you index.html JavaScript it seems as if your socket is getting the status updates directly from the JavaScript on comment box button click, shouldn’t it be getting the status updates from the mysql database? Or is it telling the socketio to do a refresh and check the database for new information because a button has been clicked?

      1. What was your problem? I am not able to get the page to refresh by adding a record to the database, I must click submit to get them all to refresh?

  3. Thank you for the tutorial. If the node modules are in the root directory of my server, then why would your code reference in the same directory as the index.html file? I assume because you are running in on a local machine. What would be the file path to I have a dedicated linux server that I have installed node and node modules on.

    Also, I don’t see where the server.js file is being referenced. Since that file has my mysql credentials in it, should I put that file in a directory outside the public_html directory, like somewhere in the root directory?

    Thanks again for your tutorial.

  4. Thank you for post but have error on server.js property “release”: >>>”C:UsersjaironoderealtimeApp>node server.js
    Listening on 3000
    A user is connected
    A user is connected
    A user is connected
    throw err;
    TypeError: Cannot read property ‘release’ of undefined
    at C:UsersjaironoderealtimeAppserver.js:39:21
    at Handshake.onConnect (C:UsersjaironoderealtimeAppnode_modulesmysqll
    at Handshake.Sequence.end (C:UsersjaironoderealtimeAppnode_modulesmysq
    at Handshake.ErrorPacket (C:UsersjaironoderealtimeAppnode_modulesmysql
    at Protocol._parsePacket (C:UsersjaironoderealtimeAppnode_modulesmysql
    at Parser.write (C:UsersjaironoderealtimeAppnode_modulesmysqllibprot
    at Protocol.write (C:UsersjaironoderealtimeAppnode_modulesmysqllibpr
    at Socket. (C:UsersjaironoderealtimeAppnode_modulesmysqlli
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)


    breaking server and not refresh page.

    1. Edit line: 13 of server.js
      database : ‘Status’,
      Edit line: 44 of server.js
      connection.query(“INSERT INTO `fbstatus` (`s_text`) VALUES (‘”+status+”‘)”,function(err,rows){

    2. Edit line: 23 of Server.js

      database : ‘Status’,

      Edit line: 44 of Server.js

      connection.query(“INSERT INTO `fbstatus` (`s_text`) VALUES (‘”+status+”‘)”,function(err,rows){

  5. I have been trying various and mySQL examples with no luck in getting any of them to work. This one seem to be the easiest, but alas, I cannot get it to work at all..

    I followed the example to the tee. When I browse to localhost:3000, I get:

    ray@NodeJS:/node/status$ sudo node server.js
    Listening on 3000
    A user is connected

    TypeError: Cannot call method ‘release’ of undefined
    at /node/status/server.js:39:22
    at Handshake.onConnect (/node/status/node_modules/mysql/lib/Pool.js:54:9)
    at Handshake.Sequence.end (/node/status/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Protocol.handleNetworkError (/node/status/node_modules/mysql/lib/protocol/Protocol.js:355:14)
    at PoolConnection.Connection._handleNetworkError (/node/status/node_modules/mysql/lib/Connection.js:355:18)
    at Socket.emit (events.js:95:17)
    at net.js:441:14
    at process._tickCallback (node.js:448:13)



  6. How do I fix this:

    ray@NodeJS:/node/status$ sudo node server.js
    Listening on 3000
    A user is connected
    A user is connected
    A user is connected
    A user is connected
    A user is connected

    TypeError: Cannot call method ‘release’ of undefined
    at /node/status/server.js:39:22
    at Handshake.onConnect (/node/status/node_modules/mysql/lib/Pool.js:54:9)
    at Handshake.Sequence.end (/node/status/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Protocol.handleNetworkError (/node/status/node_modules/mysql/lib/protocol/Protocol.js:355:14)
    at PoolConnection.Connection._handleNetworkError (/node/status/node_modules/mysql/lib/Connection.js:355:18)
    at Socket.emit (events.js:95:17)
    at net.js:441:14
    at process._tickCallback (node.js:448:13)

  7. I believe it is line 40? Anyway I already tried commenting this line out and it did not help?


  8. Is this correct in the html file:

    my script is in /node/status/node_modules/

    ray@NodeJS:/node/status/node_modules/$ pwd
    ray@NodeJS:/node/status/node_modules/$ ls index.js lib LICENSE node_modules package.json

  9. Apparently the line I wanted to include in my comment will not show up here?

    The script src=”/” is not valid on my environment

  10. So the console.log in io.on fires. I also have console.log in both of the io.on if/else cases and neither one of them are firing? So the problem appears to be somewhere in here?

    console.log(“A user is connected”);
    socket.on(‘status added’,function(status){


    1. Problem was with mysql connection which was being released on wrong place. Commenting that line in pool.getConnection() block should avoid that error. If not, can you post current error log ?

      1. (Notify) Now when I comment that line, I am getting emit error when I “Add Status”?


  11. Hi, the tutorial works well

    By the way, are there anyway to update the html page everytime data changes(Update/Insert) in MySQL?


    1. Shahid (UnixRoot) Shaikh

      Yeah they did and this post is just to show a small step about how they did that.

    2. In order to provide realtime connectivity on every browser, Socket.IO selects the most capable transport at runtime, without it affecting the API. If WebSockets are available, it will use WebSockets.

      If WebSockets aren’t available, Socket.IO will select the next best transport including: Adobe Flash Socket or Ajax Polling. So having a solid understanding of JavaScript Long Polling examples is crucial.

  12. Hi. Its works well for my ubuntu droplet.
    Is there possibilities to use TCP data instead of http data.?
    Means, i would like to send data over TCP port 3000 & store into mysql.

  13. Hello
    I tried your example. And i have some errors.
    node_modules/ not found. Which one is your actual inside node_modules.

  14. HI, very nice tutorial. But showing status to everyone doesn’t make sense. How to control who sees the updates ?? How to control the feeds? We cannot show feeds that is not subscribed by the user…
    Thanks for such a great articles ..

    Keep it up…

  15. var pool = mysql.createPool({
    connectionLimit : 100,
    host : ‘localhost’,
    user : ‘root’,
    password : ”,
    database : ‘fbstatus’,
    debug : false

    how to use these lines secure ?

  16. i want upload this file on server not on xampp localhost can you please tell me how to do this ?

  17. Andrian Bantero

    how about if i want my app not to open command line and listen to my localhost/app ?

  18. Hi,
    My sockets not working after page refresh.
    Throwing this error in console
    “Failed to load resource: net::ERR_CONNECTION_REFUSED
    VM35:10 Uncaught ReferenceError: io is not defined
    at HTMLDocument. (VM35:10)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at Function.ready (jquery.min.js:2)
    at HTMLDocument.I (jquery.min.js:2)
    localhost:3000/favicon.ico Failed to load resource: net::ERR_CONNECTION_REFUSED”
    Please help me…

    1. Shahid (UnixRoot) Shaikh

      Create an API and call it on page load.

      API should select all records from the database and send it back.

      Note: Won’t work for large data in production. Break it in streams.

  19. When i click on Add Status button it do nothing, and when i refresh the page it’s shows following error!!
    Listening on 3000
    A user is connected
    if ( {

    TypeError: is not a function
    at Ultron.remove (/opt/lampp/htdocs/socket-node/node_modules/
    at Ultron.destroy (/opt/lampp/htdocs/socket-node/node_modules/
    at WebSocket.cleanupWebsocketResources (/opt/lampp/htdocs/socket-node/node_modules/
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

  20. Great post!
    A question: if I would like to call a trigger only when a value on a mysql table become 1, instead of 0? Is it possible?

    Example: table “tblAlarms”. Tblalarms.alarm_1 is usually =1.
    I would like to callback a function, in realtime, only when tblalarms.alarm_1 become =1. Is good to do it?

    My best regards,

  21. Hello Shahid,
    Thanks for this content. Could you help me understand if I need to deploy this on staging or production server how to proceed with that ?

  22. setup client and server both but showing cors origin error. tried everything but not working.

  23. Any ideas?

    let val = process.env[key];

    SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object. (/home/qcc/ruodai/nodejsweb/node_modules/
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)

  24. Hi, I am getting these 2 error on browser console. Not sure if I am doing anything wrong
    1. GET localhost:3000/ 404 (Not Found)
    2. Uncaught ReferenceError: io is not defined

Comments are closed.