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.
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?