如何使用websockets从mysql获取实时通知更新? [英] How to get live notification updates from mysql using websockets?

查看:127
本文介绍了如何使用websockets从mysql获取实时通知更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天以来,我一直在搜索社交网络或Q/A网站如何获得实时通知.我开始了解订户-发布者模式.我知道,实时更新是使用WebSockets获取的. Websocket发布端点,客户端订阅该端点,并不断获取任何更改的更新.
但是我从网络上获得的示例都是聊天应用程序.但是我的要求是从MySQL DB获取实时通知.所以我想到的几个问题

  1. 我是否使用WebSockets来满足正确的要求?还是有 一些其他有效的方式来满足我的要求?
  2. 如果这对我来说是一种有效的方法,那么我认为可以解决的模式是:WebSocket将连接到MySql并始终查找更改.并且它还发布了我的客户将一直寻找的端点.提交给Mysql的任何更改都将反映在WebSocket中,并生成客户端应用程序.

如果我的模式正确,我不知道如何将WebSocket连接到Mysql.任何帮助/指导都将受到高度赞赏.

更新:
经过更多的网络搜索后:
1.由于我正在为我的网站创建Rest Webservices,因此AJAX是实现通知功能而不是WebSockets的更好方法吗?
2. WebSockets的实现是否比击打Rest终结点的AJAX调用复杂(因为两者的目的相同,都是为了获得通知)?

解决方案

我是否使用WebSocket满足正确的要求?

实时通知是Websockets蓬勃发展的地方,并提供了超过AJAX的巨大优势.

如您所知,在辩论 AJAX的作用之前,这已经在前面讨论过了(对于CRUD来说是很棒的,并非如此)轮询和比较时 Websocket性能与AJAX性能之间的对比(就实时更新而言,Websocket总是更快).

或者有其他有效的方法可以满足我的要求?

是的...通过向数据库访问点添加on_update钩子",可以节省资源并提高性能(以及将来的代码维护问题).

这个想法很简单:每当函数调用更新MySQL数据库时,更新请求也会发送到回调中.该回调负责将更新发布到正确的频道.

这样,您就不会轮询MySQL数据库.

某些数据库提供更新回调,而另一些则不提供.我认为MySQL确实如此.但是,我避免使用这些数据库链接的回调,因为它们是特定于数据库的.最好(IMHO)将回调添加到应用程序中的数据库访问点,因此替换数据库不会影响代码库.

  1. 自从我为网站创建Rest Webservices以来,AJAX是实现通知功能而不是WebSockets的更好方法吗?

我认为AJAX不是一个好方法.

HTTP/2有助于缓解AJAX的缺点,但并不能解决所有缺点.

我不知道您期望同时连接多少个客户端,但是强迫客户端每隔一两秒发送一次请求就非常接近自我实施的DoS攻击.

考虑一下:如果一个客户端每两秒钟发送一次AJAX请求(而不是2,000个并发客户端),则您的服务器将需要响应1,000 req/sec,包括身份验证,数据库查询和所有爵士乐.

另一方面,使用Websockets,在2,000个已连接客户端的情况下,您有2,000个持久连接在消息到达之前不执行任何操作.无需CPU或工作,只需连接的内存.在推送实际数据之前,服务器没有压力.

  1. 要实现的WebSockets是否比击打Rest端点的AJAX调用复杂(因为两者的目的相同,以获得通知)?

是的,它们的实现更加复杂,但是一旦开始,它们并不难.此外,还有许多库和帮助程序工具可以让您省去很多工作.

与Websocket方法有关的常见问题包括水平扩展的处理(通常通过添加发布/订阅数据库或服务,例如Redis),消息排序(在可能的情况下最好忽略)和数据传播问题(当我们是将数据标记为已看到"吗?是发送整个数据还是仅发出通知,说明数据可用?我们使用多少个渠道以及如何划分订阅?)

通常,答案是特定于应用程序的,并且取决于您尝试展开的功能以及数据集的预期大小(如果我在SO上给出的每个答案都是一个渠道,那么维持它是不现实的). /p>

无论如何...祝你好运!

From few days I was searching how social network or Q/A websites get live notifications. And I came to know about the subscriber-publisher pattern. I came to know that the live updates are fetched using the WebSockets. Websocket publish an endpoint and the client subscribe to that endpoint and constantly get updates for any changes.
But the examples I got from the web are all of chat applications. But my requirement is to get live notifications from MySQL DB. So I have few questions in mind

  1. Am I using the WebSockets for the correct requirement? Or there is some other efficient way for my requirement?
  2. If this is the efficient way for me, the pattern what I think to resolve this is: The WebSocket will be connected to the MySql and always looks for the changes. And It also publishes an endpoint to which my client will always look for. Any changes committed to Mysql will be reflected in WebSocket and resulting in the client application.

If my pattern is right, I don't know how can I connect the WebSocket to Mysql. Any help/guidance is highly appreciated.

update:
After some more web searching:
1. Since I am creating the Rest Webservices for my website, is AJAX the better way to implement notification feature instead of WebSockets?
2. Are WebSockets complex to implement than AJAX calls hitting Rest endpoint ( since the purpose of both is same, to get notifications)?

解决方案

Am I using the WebSockets for the correct requirement?

Live notifications is where Websockets thrive and provide a huge advantage over AJAX.

As you know, this was already discussed before both when debating the role of AJAX (great for CRUD, not so much when polling) and when comparing Websocket performance vs. AJAX performance (Websockets are always faster where live updates are concerned).

Or there is some other efficient way for my requirement?

Yes... you could save resources and improve performance (as well as future code maintenance issues) by adding on_update "hooks" to the database access point.

The idea is simple: whenever a function call updates the MySQL database, the update request is also sent to a callback. That callback is in charge of publishing the update to the correct channel.

This way, you don't poll the MySQL database.

Some databases offer update callbacks and others don't. I think MySQL does. However, I avoid these database linked callbacks because they are database specific. It's better (IMHO) to add the callback to the database access point in the application, so replacing a database doesn't effect the code-base.

  1. Since I am creating the Rest Webservices for my website, is AJAX the better way to implement notification feature instead of WebSockets?

I don't think AJAX is a good approach.

HTTP/2 helps to mitigate the AJAX shortcomings, but it doesn't solve all of them.

I don't know how many clients you expect to be concurrently connected, but forcing a client to send a request every second or two is very close to a self inflicted DoS attack.

Consider this: if a client sends an AJAX request every two seconds, than at 2,000 concurrent clients, your server will need to respond to 1,000 req/sec - these include authentication, database queries and all that jazz.

On the other hand, using Websockets, with 2,000 connected clients, you have 2,000 persistent connections doing nothing until a message arrives. No CPU or work required, just the connection's memory. There's no stress on the server until actual data is pushed.

  1. Are WebSockets complex to implement than AJAX calls hitting Rest endpoint (since the purpose of both is same, to get notifications)?

Yes, they are more complex to implement, but they aren't that hard once you start. Also, there's a lot of libraries and helper tools that take much of the work off your shoulders.

Common issues related to the Websocket approach include the handling of the horizontal scaling (often by adding a pub/sub database or service, such as Redis), message ordering (which is better ignored when possible) and data propagation concerns (when do we mark data as "seen"? do we send the whole data or just a notification stating that data is available? how many channels do we use and how do we divide subscriptions?).

Usually the answers are application specific and depend on the feature you're trying to unroll as well as the expected size of your dataset (if every answer I gave on SO was a channel, it would be unrealistic to maintain).

Anyway... Good Luck!

这篇关于如何使用websockets从mysql获取实时通知更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆