简体   繁体   English

从数据库中获取字段的实时值(Mysql)

[英]Get the real-time value of a field from a database (Mysql)

I have created a website with HTML5, CSS3, Javascript / jquery, PHP, MySQL... and I am considering implementing a functionality in real time. I have created a website with HTML5, CSS3, Javascript / jquery, PHP, MySQL... and I am considering implementing a functionality in real time.

It would be that, depending on the value of a database field (mysql), display one image or another on the web.这将是,根据数据库字段(mysql)的值,在 web 上显示一个或另一个图像。 I have also thought about creating a notification system: if a mysql field has a certain value, send a notification to the user.我还考虑过创建一个通知系统:如果 mysql 字段具有一定的值,则向用户发送通知。

The problem is in how to do it in real time without the user having to do anything.问题在于如何在无需用户做任何事情的情况下实时进行。 I have thought that maybe websockets could be a good option but I have never used websockets / Nodejs and I don't know if it would be cumbersome to implement / use it.我认为也许 websockets 可能是一个不错的选择,但我从未使用过 websockets / Nodejs,我不知道实现 / 使用它是否会很麻烦。

The web can sometimes have around 1,000 users at the same time (I use a simple vps server) web 有时可以同时拥有大约 1,000 个用户(我使用简单的 vps 服务器)

Any advice?有什么建议吗?

Thank you!谢谢!

Websockets work with events and listeners. Websockets 与事件和监听器一起工作。 You can make an endpoint in your backend and do the logic to get the field you need from the database and emit an event that sends that value to the frontend.您可以在后端创建一个端点并执行逻辑以从数据库中获取您需要的字段并发出一个将该值发送到前端的事件。 Then put a listener there to obtain the information you are sending and thus maintain communication in real-time.然后在此处放置一个侦听器以获取您发送的信息,从而保持实时通信。

Learn more here https://socket.io/在此处了解更多信息https://socket.io/

Use mysql-events https://www.npmjs.com/package/mysql-events package.使用 mysql-events https://www.npmjs.com/package/mysql-events package。 this watches a MySQL database and runs callbacks on matched events.这会监视 MySQL 数据库并在匹配事件上运行回调。 basic examples is follows from documentation基本示例来自文档

var watcher =mysqlEventWatcher.add(
 'myDB.table.field.value',
 function (oldRow, newRow, event) {
    //row inserted
   if (oldRow === null) {
     //insert code goes here
   }

    //row deleted
   if (newRow === null) {
    //delete code goes here
   }

 //row updated
if (oldRow !== null && newRow !== null) {
  //update code goes here
}

//detailed event information
//console.log(event)
 }, 
    'match this string or regex'
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM