3

Guys it's been a long time I have been working on my web project.I am creating a real time notification system for now and I am using node js + socket io.I am happy that with the help of expert people on stackoverflow I was able to work apache server and nodejs server on different port together working for one whole web application.Ok now let's go to the point.

With my logic guys I was trying to make a real time notification by two steps :-

1) First a user connect on socket via client side page then it emit connection event which is handled by our nodejs server file.

2) After detecting a user been connected on a socket I performed some mysql fetch inside that nodejs made server file like :-

// ....all module are presented here above...

io.sockets.on('connection', function(client) {
  var pool = mysql.createPool({
    connectionLimit: 10,
    host: "localhost",
    user: "root",
    password: "",
    database: "mydatabase"
  });

  pool.getConnection(function(err, connection) {
    var queryString = "Select * from table_one";
    connection.query(queryString, function(error, results) {
      if (error) {
        throw error;
      } else {
        console.log("New client !");
        io.sockets.emit('u_d', results);
      }
    });
    connection.release();
  });
});

Above there I have used nodejs mysql module.It fetches all data from that table name table_one on json data type like in this picture below.

Mysql fetched data retrieved on json format

You can see I got my fetched data on json type.Is it possible to make my json data get in proper format like this :-

{
  "result": [{
      "id": "2",
      "user": "hari061",
      "pic": "img.jpg"
    },
    {
      "id": "3",
      "user": "ram061",
      "pic": "nature.jpg"
    }
  ]
}

That json data will be handed to my client side here :-

var socket = io.connect('http://localhost:3000');

socket.on('u_d', function(data) {
  $('.display_noti').append($('<li style="margin-bottom:10px;margin-left:10px; margin-right:10px;">').text(data));
});

After all this I found two problem :-

1) In each connection of a user mysql fetches data.(Which mean my user should connect on a socket every time for a new update).It will not be a real time I mean a page reload can only make that user connect on a socket back then only it will display him/her new update.

2) As I found mysql fetched data are retrieve on json format.I want to control the outcome of json format suitable for my client side.

how can I solve this two problem guys?

Shanoor
  • 13,344
  • 2
  • 29
  • 40
SOuřaan Gřg
  • 399
  • 4
  • 16

1 Answers1

1

If you modify data outside your server you can add some RESTful API to trigger server that data is updated:

# example with express.js
app.put '/newdata/', (req, res) ->
    get_updated_data (result) ->
        io.sockets.emit 'u_d', result       # send new update to all connected clients

At this logic when user connects you should send "last state" of data to new client:

io.sockets.on 'connection', (client) ->
    get_last_state (result) ->
        client.emit 'last state', result    # send last state to new client

Simple JSON formatting in one line:

JSON.stringify jsonObject, null, '\t'

And here you can read more how to beautify JSON string.

Also, if you place connection call to MySQL outside of io's connection you'll save resources on MySQL connect/disconnect each time client connects to the socket.

Community
  • 1
  • 1
iw2rmb
  • 118
  • 9
  • I am confuse how can I make a user show notification update in real time?I mean I have fetched data from database and handed it on client but its working for one time only.How should server and client code work to achieve real time feature? – SOuřaan Gřg Jan 18 '16 at 14:28
  • If you have access to code that changes data then you have to place there a request call (in npm see `request`) to let server know that your data has changed. Then you perform actions you need and send information about update to all connected clients. In some cases it's enough to fetch table with some interval, compare to previous fetch result and send changes. But it's a bad choice in many ways. – iw2rmb Jan 18 '16 at 14:35
  • I want such a logic bro I first wanna fetch some data from a table and emit those data back to the client.I then want if there is any new changes happens on that table it immediately emit again to the client so, that the user get new update in real time.Is there any way to to that bro? – SOuřaan Gřg Jan 18 '16 at 14:52
  • Short answer — yes. Do you modify your table data on server or outside of the server? – iw2rmb Jan 18 '16 at 15:05
  • why is it concern to modify bro?Can't i do some emit after i fetched data from database?You can see bro i'm trying to create emit after I fetched data from database. – SOuřaan Gřg Jan 18 '16 at 18:10
  • 1
    Because fetching on connection will give you data in the moment. After that you have to know somehow that there is an update to send it to client. – iw2rmb Jan 19 '16 at 06:52
  • ok then @iw2rmb what will be the best idea to make it work?? – SOuřaan Gřg Jan 19 '16 at 11:08
  • To answer that I need to know where and how your data is modifying? – iw2rmb Jan 19 '16 at 11:11
  • ok then bro i got what you mean.I guess i should emit when ever some user create some action right? – SOuřaan Gřg Jan 19 '16 at 11:31
  • Yes. But you need to be more specific in your questions. – iw2rmb Jan 19 '16 at 11:40
  • Can u edit my question title bro if you understand what i mean. – SOuřaan Gřg Jan 19 '16 at 11:44