I made a simple application similar to your requirement.
You can get help from the following lines of code. You need to understand the basics of code. after that you will easily reach your goal. most of your requirements are described in this demo application.
This is not accurate, but you will meet your goal through this.
In this example, the status message of any user will be displayed to all other users at the same time. we can manipulate it to achieve "1 new status."
create a table in the database where your records will be saved CREATE TABLE status
( `status_id` INT NOT NULL AUTO_INCREMENT, `s_text` TEXT, `t_status` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ( `status_id` ) );
//server.js
var app = require("express")(); var mysql = require("mysql"); var http = require('http').Server(app); var io = require("socket.io")(http); var pool = mysql.createPool({ connectionLimit: 100, host: 'localhost', user: 'root', password: '', database: 'fbstatus', debug: false }); app.get("/", function(req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', function(socket) { console.log("A user is connected"); socket.on('status added', function(status) { add_status(status, function(res) { if (res) { io.emit('new status', status); } else { io.emit('error'); } }); }); }); var add_status = function(status, callback) { pool.getConnection(function(err, connection) { if (err) { connection.release(); callback(false); return; } connection.query("INSERT INTO `status` (`s_text`) VALUES ('" + status + "')", function(err, rows) { connection.release(); if (!err) { callback(true); } }); connection.on('error', function(err) { callback(false); return; }); }); } http.listen(3000, function() { console.log("Listening on 3000"); });
//index.html
<html> <head> <title>Socket.io</title> <script src="/socket.io/socket.io.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> <script src = "http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <script> $(document).ready(function(){ var socket = io(); $("#add_status").click(function(){ socket.emit('status added',$("#comment").val()); }); socket.on('new status',function(msg){ var count = $('#count_status').text(); var valCount = parseInt(count); if(valCount>=1) { valCount = valCount+1; } else { valCount = 1; } var showMsg = '<div id="count_status"> '+valCount+' </div> new status'; $("#show_comments").html(showMsg); }); }); </script> </head> <body> <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"> </div> <div id= "show_comments" class = "jumbotron"></div> </body> </html>
Run the application with the following node Server.js command
Now run http: // localhost: 3000 / in the browser and to see the result, a new window will open in which you publish the status and see the new notification status in both windows.
thanks
Edited: This is a great start-up tutorial. several things need modification.
- connection.release () the code ends up unreadable and does not work. you must comet or delete it.
2. Actual result in my case: 