使用node.js和socket.io向用户显示实时数据

发布于 2024-11-03 21:45:28 字数 701 浏览 4 评论 0原文

我正在寻求将我们的 Web 应用程序从传统的 PHP/MySQL 优化/升级到更高效的技术。我一直在研究 node.js、socket.io 和 CouchDB,它们看起来很有前途,但我正在寻找建议和一些帮助来选择我们想要的最佳技术。

那么,概述一下当前所做的事情...

我们拥有捕获数据并将其发送到 FTP 服务器(通过 .txt 文件)的设备。 PHP 脚本每 5 分钟运行一次,扫描 FTP 以查找设备发送的 .txt 文件。文件的内容将插入 MySQL 数据库,并删除 .txt 文件。另一个每 15 分钟运行一次的 PHP 脚本将从数据库中整理数据(例如,上一小时的总和值)并生成一个 XML 文档,该文档由 Flash dial 读取。

显然,这里存在很多问题,而且距离实时解决方案还很远,而这正是我们的目标。我们想要的解决方案是,以某种方式,它可以检测 txt 文件何时上传到 FTP,然后仅处理数据(而不是设置运行间隔)。数据仍然需要添加到数据库中,添加到数据库后立即被前端读取。 Flash 将被完全删除,因为它有点……恶心……

有了令人兴奋的新技术,如 Node.js 和 Web Sockets (socket.io),我相信我们可以极大地改进这个过程!我知道 Ajax 能够执行类似的操作,但听说与 Web Sockets 相比,它的开销相当高。我对数据库如何与 Node.js 一起工作也有点模糊,更不用说我们所追求的最佳选择......

谢谢!

I'm looking to optimize/upgrade our web application from the traditional PHP/MySQL, to a more efficient technology. I've been researching node.js, socket.io and CouchDB, which look promising, but am looking for recommendations and some help in choosing the best technologies for what we want.

So, to outline the what is currently done...

We have devices that capture data and sends it to an FTP server (via .txt file). A PHP script runs every 5 minutes, scanning the FTP for .txt files that the devices send. The contents of the files are inserted into a MySQL database, and the .txt file deleted. Another PHP script that runs every 15 minutes will collate data from the database (eg, sum values for last hour) and produce an XML document, which are read by Flash dials.

Obviously, there are plenty of issues here, and it is far from a real-time solution, which is what we are aiming for. The solution we'd like is, that somehow, it could detect when the txt file is uploaded to the FTP, and only process the data then (rather than having a set interval that runs). The data would still need to be added to the database, and be read by the front-end as soon as it is added to the database. Flash would be removed entirely, as its a little...ick...

With exciting new technologies like node.js and Web Sockets (socket.io), i'm sure we could improve this process greatly! I'm aware of Ajax being able to do something like this, but hear it has rather high overheads compared to Web Sockets. I'm also a little hazy with how a database would work with node.js, not to mention the best option for what we're after...

Thanks!

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

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

发布评论

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

评论(1

转瞬即逝 2024-11-10 21:45:28

Node 与 NoSQL 配合得很好(尤其是基于 JSON 的 NoSQL),但由于它们是用于处理 Node 中大多数异步数据库并返回 javascript 对象的模块,如果您更喜欢使用 MySQL,我不会放弃使用它。

这是一个与数据库无关的大纲。

var fs = require('fs');  //to watch the FTP
var db = require('db');  //your choice of db
var express = require('express'); //http server
var io = require('socket.io'); //for realtime data push
var app = express.createServer(); //create http server
/*...usual express implementation...*/
app.listen(80);
var socket = io.listen(app); 
db.connect( ..., start );
function checkForFiles () {
  fs.readdir( FTPpath, sendFilesToDB );
};
function sendFilesToDB ( err, files ) {
  if( files.length === 0 ) {
    return checkForFiles();
  }
  db.insert( fileToQuery( files.pop() ), function () {
    sendFilesToDB( err, files );
  });
}
function fileToQuery ( file ) {
  ...
  return query;
}
function start () {
  checkForFiles();
  setTimeout( checkData, 1000 );
}
function checkData () {
  db.query( '....', function ( err, data ) {
    socket.broadcast( processData( data ) );
    setTimeout( checkData, 1000 );
  }
}
function processData (data) {
  ...
  return data;
}

Node works well with NoSQL, (especially JSON based NoSQL) but since their are modules for handling most databases in node that are async and return javascript objects I wouldn't dismiss using MySQL if you're more comfortable using it.

here's an database agnostic outline.

var fs = require('fs');  //to watch the FTP
var db = require('db');  //your choice of db
var express = require('express'); //http server
var io = require('socket.io'); //for realtime data push
var app = express.createServer(); //create http server
/*...usual express implementation...*/
app.listen(80);
var socket = io.listen(app); 
db.connect( ..., start );
function checkForFiles () {
  fs.readdir( FTPpath, sendFilesToDB );
};
function sendFilesToDB ( err, files ) {
  if( files.length === 0 ) {
    return checkForFiles();
  }
  db.insert( fileToQuery( files.pop() ), function () {
    sendFilesToDB( err, files );
  });
}
function fileToQuery ( file ) {
  ...
  return query;
}
function start () {
  checkForFiles();
  setTimeout( checkData, 1000 );
}
function checkData () {
  db.query( '....', function ( err, data ) {
    socket.broadcast( processData( data ) );
    setTimeout( checkData, 1000 );
  }
}
function processData (data) {
  ...
  return data;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文