Web 上的数据流

发布于 2024-11-04 14:38:05 字数 1301 浏览 1 评论 0原文

背景

因此,如果您访问 https://foursquare.com/,然后查看在“近期活动”标题中,您将看到来自世界各地的 Foursquare 各个成员的不断更新的更新列表。

新的更新滑到堆栈的顶部,最旧的更新滑落(可能使用 JQuery 或其他东西,但这不是我的问题)。

Stack Overflow 的另一位成员告诉我,这实际上或多或少是一个伪新闻源。当您加载页面时,它会调用数据库并获取 30 条最新记录,然后最初在页面上显示这 30 条记录中最旧的 11 条,然后设置超时以滑出最旧的记录/在最新的记录上滑动。然而,它只是一致地循环访问这 30 条记录(如果您将自己留在页面上而不刷新或执行任何操作,您会注意到这一点)。它不会进行任何其他 AJAX 调用来从服务器获取新消息。这仅在页面加载时发生。

问题

  1. 他们这样做有什么原因吗?
  2. 我希望我的设置能够在页面加载时抓取一组记录,并显示它们(类似于 Foursquare),但是,我希望(在循环浏览我带来的记录之后)到 DOM),然后进行另一个 AJAX 调用以查看是否有更新的内容,并将这些新内容无缝集成到同一流程中。基本上,可以将其视为 Twitter 和 Foursquare 之间的混搭。 Twitter 会让你知道何时有新消息,并让你可以选择将它们弹出到你的 feed 顶部,而 Foursquare 只是在页面刷新时引入新消息,仅此而已。开始思考这个问题的方法是什么(在效率、架构等方面)?

我也准备听听是否有任何理由让我不应该这样做。

到目前为止已完成

到目前为止,我有 SQL Server 存储过程,可以让我获取一组记录(来自网站上给定的一组情况 - 偏离主题)。我可以随时调用这些(显然)。从数据库返回的这些记录的一部分是它们的唯一 ID。当我在 DOM 中显示它们时,我使用 标签来保存每条消息的 ID,因此我能够向服务器发出 AJAX 回调并传入向用户显示的最后一个 ID,然后我还有一个过程,它只会返回具有更大 ID 的 ID(稍后输入)。

正如你所看到的,就技术上是否可行而言,我知道是这样。我可以在 DOM 中的页面加载时显示一大堆消息,然后一旦到达最后一条消息,就对服务器进行另一个 AJAX 调用以在顶部弹出更多消息,并不断更新它们。我主要问上面的两个问题,这是一个好主意吗(在 DOM 中同时拥有那么多消息)?如果有任何更好、更有效的方法我可以做到这一点。

谢谢你们。

Background

So, if you go to https://foursquare.com/, and look under the header "Recent Activity", you will see a constantly updating list of updates from various members of Foursquare all over the world.

New updates slide onto the top of the stack, and the oldest one slides off (probably using JQuery or something, but that's not my question).

I was informed by another member of Stack Overflow that this is actually more or less a pseudo news feed. When you load the page, it makes a call to the database and grabs the 30 newest records, and then displays the 11 oldest out of that 30 on the page initially, and then sets a timeout to slide off the oldest/slide on the newest. However, it is only consistently looping through these same 30 records (you will notice this if you leave yourself on the page without refreshing or anything). It will not make any other AJAX calls to get new messages from the server. This only happens when the page loads.

Question(s)

  1. Is there a reason that they are doing that?
  2. I want mine to be setup so I can grab a set of records when the page loads, and display them (similar to Foursquare), however, I would like (after I loop through the ones that I brought into the DOM), to then make another AJAX call to see if there are any newer, and seamlessly integrate these new ones into the same flow. Basically, think of this as a mash-up between Twitter and Foursquare. Twitter lets you know when there are new messages, and gives you the option to pop them on top of your feed, and Foursquare just brings the new ones in on a page refresh, and that's it. What would be a way to start thinking about this (in terms of efficiency, architecture, etc.)?

I'm up for also hearing if there are any reasons why I shouldn't be doing this.

Completed So Far

So far I have SQL Server stored procedures that let me obtain a set of records (from a given set of a circumstances on the site - off topic). I can call these whenever (obviously). As part of these records coming back from the database, is their unique ID. When I display them in the DOM, I am using a <span> tag to hold the ID of each message, so I have the ability to make an AJAX call back to the server and also pass in the last ID that was shown to the user, and then I also have a proc that will only return ones with a greater ID (entered later).

So as you can see, as far as if it's technically possible, I know it is. I can display a whole bunch of messages on page load in the DOM, and then as soon as it gets to the last one, make another AJAX call to the server to pop more on top, and keep updating them. I'm mainly asking the two questions above, is this a good idea (to have that many messages in the DOM at once)? And if there are any better, and more efficient ways I can be doing this.

Thanks guys.

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

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

发布评论

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

评论(1

红颜悴 2024-11-11 14:38:05

到目前为止,AJAX 调用可能是最有效的。一旦Web Sockets最终停止变形和调整版本,这将是这样去。查看我的问题和优秀的答案(请务必阅读评论):打开到服务器的 TCP 流的最佳方式是什么?

至于您的主要问题,这是一个好主意吗?当然。现在理所当然,进行大量 AJAX 调用的大公司拥有漂亮的大型服务器机房,里面装满了从事肮脏工作的服务器。如果您没有这种服务器能力,您可以考虑在计时器上进行 AJAX 调用,例如每分钟左右进行一次,以减少服务器压力。
有更好的办法吗?事实并非如此,直到 Web Sockets(见上文)完全发展为止。
AJAX 调用对我来说似乎是最好的选择(通过非常简单的 jQuery.ajax 方法发出 AJAX 请求,通过服务器返回 JSON,用 jQuery 解析并填充到 DOM 中。非常好,真的。)

As of yet, AJAX calls are probably the most efficient. Once Web Sockets finally stops morphing and shfiting versions, it'll be the way to go. Check out my question on this and the excellent answer (be sure to read the comments) on it: What's the Best Way to Open a TCP Stream to Server?

As to your main questions, is it a good idea? sure. Now granted, giant corporations that make gazillions of AJAX calls have nice big server rooms full of servers doing the dirty work. If you don't have that kind of server power, you might consider making AJAX calls on a timer, say, every minute or so, to reduce server strain.
Is there a better way? Not really, until Web Sockets (see above) evolves completely.
AJAX calls seem the best choice to me (making AJAX request via the very easy jQuery.ajax method, returning via the server in JSON, parsing with jQuery and filling into the DOM. very nice, really.)

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