[英]How to implement push notification system on a mysql database with node.js
I'm totally new to node.js
and I want to implement push notification system on a MySql
database. 我是
node.js
新手,我想在MySql
数据库上实现推送通知系统。 I have a notification table in my database. 我的数据库中有一个通知表。 In this table I have store
recipient_id
that specify the recipient of the notification. 在此表中,我有
recipient_id
,用于指定通知的收件人。 Now I want when a new notification with recipient_id
is equal to current logged in user's id notify that user. 现在我想要一个带有
recipient_id
的新通知等于当前登录用户的id通知该用户。 Something like Stackoverflow
If you are in the for example java
tagged questions, every time a new question with java
tag create, a notification appear on top of the page : 1 question with new activity.
像
Stackoverflow
这样的东西如果您在例如java
标记的问题中,每次创建带有java
标记的新问题时,页面顶部都会显示一条通知: 1 question with new activity.
Sorry for my poor English. 抱歉我的英语不好。 Please help me to implement this system, because I'm new to it.
请帮我实现这个系统,因为我是新手。
I have made a simple app like your requirement. 我已经制作了一个像你的要求的简单应用程序
You can get help from following lines of code.You need to understand the basics of code. 您可以从以下代码行获得帮助。您需要了解代码的基础知识。 after that you will easily achieve your target.
之后,您将轻松实现目标。 most of things from your requirement covered in this demo app.
本演示应用程序中涵盖了您的要求中的大部分内容。
Its not a exact but you will meet your target through this. 它不是一个确切的但你会通过这个达到你的目标。
In this example a status post by any user will emit to all other users also at same time. 在此示例中,任何用户的状态帖子也将同时向所有其他用户发出。 we can manipulate it to achieve "1 new status".
我们可以操纵它来实现“1新状态”。
make a table in database where your entries to be saved CREATE TABLE status
在数据库中创建一个表,在其中保存要保存的条目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
//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
//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 app with following command node Server.js 使用以下命令节点Server.js运行应用程序
Now run http://localhost:3000/ in browser and to see the result open a new window in which you post a status and see your new status notification in both the window. 现在在浏览器中运行http:// localhost:3000 /并查看结果,打开一个新窗口,在该窗口中发布状态并在窗口中查看新状态通知。
Thanks 谢谢
Edited: This a great startup tutorial. 编辑:这是一个很棒的启动教程。 a few thing needs modification.
有些事情需要修改。
You can do it 2 ways: 你可以通过两种方式做到:
websockets
which maintains a permanent connection between your client and server, and then you can send notifications to the client from your server code in real-time. websockets
来维护客户端和服务器之间的永久连接,然后您可以通过服务器代码实时向客户端发送通知。 See socket.io
tutorials. socket.io
教程。 This is called a push strategy.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.