Node.js 中的 MVC / websocket

发布于 2024-12-19 05:09:32 字数 813 浏览 6 评论 0原文

我正在考虑构建一些使用 Websocket 的 Intranet 应用程序。我目前正在服务器上使用 Python/Pylons 作为我的 Web 框架,并进行轮询以更新页面 DOM 中的项目。 Pylons 不太适合与 websocket 通信(恕我直言),因为它每个连接使用一个线程。我正在考虑使用 node.js 作为服务器来与我的 Web 应用程序中的 websocket 连接进行通信。这是我的应用程序的“10,000 英尺视图”:

  • Pylons 提供 Web 内容(html、css、图像、javascript 等)
  • 页面应用程序上的 JavasSript 向 node.js 服务器打开 websocket Node.js
  • 服务器通过 websocket 将数据推送到应用程序
  • JavaScript 根据来自 websocket 的数据更新页面 DOM 元素

上面案例中的数据来自 MySQL 数据库,这就是我的问题的来源。我之前已经设置过 MVC 类型的应用程序,并且可以在 node.js 中执行相同的操作。但是,如果我有一个向 Node.js 服务器开放的长期 Websocket,那么 Node.js 如何了解模型中的更改并将它们推送到应用程序?例如,如果我想更新 Web 应用程序页面上显示的总计,并且这些总计由于 Node.js(其他 Web 应用程序)之外的系统中的操作而发生变化,那么 Node.js 如何通知这些更改?我想到的是让 Node.js 轮询数据库中的各种更改并将更改传播到各种视图。但对我来说,这听起来像是我正在将轮询从 Web 应用程序转移到 Node.js 服务器?

有人对此有任何想法、建议或指示吗?

提前致谢! 道格

I'm thinking about building some intranet applications that make use of websockets. I'm currently using Python/Pylons for my web framework on the server, and doing polling to update items in the DOM of the page. Pylons is not well suited to communicate with websockets (IMHO) as it uses a thread per connection. I'm considering using node.js as the server to communicate with the websocket connections from my web application. Here's the "10,000 foot view" of my application:

  • Pylons delivers the web content (html, css, images, javascript, etc.)
  • JavasSript on the page application opens up websocket(s) to the node.js server
  • The node.js server pushes data to the application through the websocket
  • JavaScript updates the page DOM elements based on the data from the websocket

The data in the case above comes from a MySQL database, which is where my question comes from. I've set up MVC type applications before, and can do the same kind of thing in node.js. However, if I have a long lived websocket open to the node.js server, how does node.js become aware of changes in the Model and push them out to the application? For instance if I want to update totals presented on the web application page, and those totals change due to actions in the system outside of node.js (other web applications), how is node.js notified of those changes? The thing that comes to mind is to have node.js poll the database for various changes and propagate the changes to the various Views. But to me that just sounds like I'm moving my polling from the web application to the node.js server?

Anyone have any ideas, suggestions or pointers on this?

Thanks in advance!
Doug

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必了 2024-12-26 05:09:32

您可以:

  • 让 Python 脚本通知 node.js 应用程序(通过套接字或通过 HTTP)
  • 或从 node.js 进行轮询,因为它不知道其环境之外的更改 轮询

被认为是不好的,因为它无法扩展。当有一个轮询的进程确实可以扩展时,因为当另一个用户连接时它不需要更多的连接。基本上:通过

// query every second or so
setInterval(function () {
    // query database
    doSomeDatabaseStuff(function (res) {
        // check dirty
        if (res.changed) {
             // notify all clients
             allConnectedSockets.forEach(function (socket) {
                 socket.send({ msg: "update" });
             });
        }
    })
}, 1000);

这种方式,您可以使用一个进程轮询数据库,并使用一个可扩展的架构来通知连接的客户端。数据库仍然可以从任何来源填充。

You can either:

  • Let the Python scripts notify the node.js application (via a socket or via HTTP)
  • Or poll from node.js because it is not aware of changes outside it's environment

Polling is considered bad because it doesn't scale. When having a single process that polls does scale because it doesn't need more connections when another user connects. So basically:

// query every second or so
setInterval(function () {
    // query database
    doSomeDatabaseStuff(function (res) {
        // check dirty
        if (res.changed) {
             // notify all clients
             allConnectedSockets.forEach(function (socket) {
                 socket.send({ msg: "update" });
             });
        }
    })
}, 1000);

This way you have one single process polling the database, and a scalable architecture to notify your connected clients. The database can still be filled from any source.

〃温暖了心ぐ 2024-12-26 05:09:32

Sails.js 是一个 Node 的 MVC 框架,它的独特之处在于以与 Express 路由相同的方式以 REST 方式路由 Socket.io 消息。

Sails 目前使用 Sequelize,并且默认配置为使用 mySQL(但也支持 SQLite 和 Postgres)。我们正在切换到一个模型,让您选择自己的 ORM,这将使我们能够支持 JugglingDB(它增加了对 Mongo 等的支持)

它还捆绑了一个前端组件 Mast。 Mast 提供与 Meteor 类似的功能,它允许您从客户端直接与数据库对话。如果需要提供更复杂的功能,只需在后端添加一个控制器即可。

更多信息请参见:https://github.com/balderdashy/sails

Sails.js is an MVC framework for node that has the unique distinction of RESTfully routing Socket.io messages in the same way as your Express routes.

Sails currently uses Sequelize, and is configured by default to use mySQL (but also supports SQLite and Postgres). We're switching to a model that lets you choose your own ORM, which will allow us to support JugglingDB (which adds support for Mongo, amongst others)

It also comes bundled with a front-end component, Mast. Mast provides similar functionality to Meteor, in that it allows you to talk directly to the database from the client. If you need to provide more complex functionality, you just add a controller on the backend.

More here: https://github.com/balderdashy/sails

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文