How to implement push notification system in mysql database using node.js

I am completely new to node.js and I want to implement a push notification system in the MySql database. I have a notification table in my database. In this table, I have a recipient_id repository that indicates the recipient of the notification. Now I want when the new notification with recipient_id is equal to the current registered user ID, inform the user about it. Something like Stackoverflow If you put tags in the java tag, each time a new question is created with the java tag, a notification appears above the top of the page: 1 question with new activity. Sorry for my poor English. Please help me implement this system because I am new to it.

+6
source share
2 answers

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); /* Creating POOL MySQL connection.*/ 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: daniel adenew software engineer

+4
source

You can do this in two ways:

  • Request a server every n seconds for any new messages. Skip the timestamp when you last checked as a parameter, and if there is any notification since the last check, return as json and display them in the client. This is called a pull strategy.
  • Or you can use websockets , which maintains a constant connection between your client and server, and then you can send notifications to the client from your server code in real time. See socket.io tutorials. This is called a push strategy.
+1
source

Source: https://habr.com/ru/post/984382/


All Articles