我应该使用AJAX还是WebSockets ?

时间:2021-03-20 02:58:04

Oh the joyous question of HTTP vs WebSockets is at it again, however even after quit a bit of reading on the hundreds of versus blog posts, SO questions, etc, etc.. I'm still at a complete loss as to what I should be working towards for our application. In this post I will be supplying information on application functionality, and the types of requests/responses used in our application currently.

哦,HTTP vs WebSockets这个令人高兴的问题又来了,然而,即使在几百篇相对于博客文章的阅读之后,诸如此类的问题,等等。我仍然完全不知道我应该为我们的申请做些什么。在这篇文章中,我将提供关于应用程序功能以及当前应用程序中使用的请求/响应类型的信息。

Currently our application is a sloppy piece of work, thrown together using AngularJS and AJAX requests to a Apache server running PHP, namely XAMPP. With the launch of our application I've noticed that we're having problems with response times when the server is under any kind of load. This probably has something to do with the sloppy architecture of our server, the hardware, and the fact that our MySQL database isn't exactly optimized.

目前,我们的应用程序是一项草率的工作,使用AngularJS和AJAX请求将其组合到运行PHP(即XAMPP)的Apache服务器上。随着应用程序的启动,我注意到当服务器处于任何类型的负载时,我们的响应时间都有问题。这可能与服务器、硬件的松散架构以及MySQL数据库没有经过精确优化有关。

However, with such a loyal fanbase and investors seeing potential in our application and giving us a chance to roll out a 2.0 I've been studying hard into how to turn this application into a powerhouse of low latency scalability. Honestly the best option would be hire someone with experience, but unfortunately I'm a hobbyist, and a one-man-army without much experience.

然而,有了这么多忠实的粉丝和投资者,他们看到了我们的应用程序的潜力,并给了我们一个推出2.0的机会。老实说,最好的选择是雇一个有经验的人,但不幸的是,我是个业余爱好者,一个没有多少经验的人。

After some extensive research, I've decided on writing the backend using NodeJS this time. However I'm having a hard time deciding on HTTP or Websockets. Here's the types of transactions that are done between the Server/Client.

经过广泛的研究,这次我决定使用NodeJS编写后端。然而,我很难决定使用HTTP或Websockets。以下是在服务器/客户端之间完成的事务类型。

  • Client sends a request to the server in JSON format. The request has a few different things.

    客户端以JSON格式向服务器发送请求。这个请求有一些不同的东西。

    • A request id (For processing logic based on the request)
    • 请求id(用于基于请求的处理逻辑)
    • The data associated with the request ID.
    • 与请求ID相关联的数据。
  • The server receives the request, polls the database (if necessary) and then responds to the client in JSON format. Sometimes the server is serving files to the client. Namely images in Base64 format.

    服务器接收请求,轮询数据库(如果需要),然后以JSON格式响应客户机。有时,服务器正在向客户端提供文件。即以Base64格式的图像。

Currently the application (When being used) sends a request to the server every time an interface is changed, which on average for our application is once every few seconds. Every action on our interfaces sends another request to the server. The application also sends requests to check for notifications/messages every 8 seconds, (or two seconds depending on if they're on the messaging interface).

当前,应用程序(在使用时)在每次更改接口时向服务器发送一个请求,对于我们的应用程序来说,平均每隔几秒钟发送一次请求。接口上的每个操作都向服务器发送另一个请求。应用程序还会每8秒发送一次请求来检查通知/消息(或者根据消息接口的不同,每2秒检查一次)。

Currently here are the benefits I see of a stated connection over a stateless connection with our application.

下面是我看到的与应用程序的无状态连接的声明连接的好处。

  • If the connection is stated, I can eliminate the requests for notifications and messages, as the server can just tell the client whenever one comes available. This can eliminate x(n)/4 requests per second to the server alone.

    如果连接被声明,我可以消除通知和消息的请求,因为只要客户机可用,服务器就可以告诉客户机。这可以消除每秒对服务器的x(n)/4个请求。

  • Handling something like a disconnection from the server is as simple as attempting to reconnect, opposed to handling timeouts/errors per request, this would only be handled on the socket.

    处理从服务器断开连接之类的事情就像尝试重新连接一样简单,而不是处理每个请求的超时/错误,这将只在套接字上处理。

  • Additional security can be obtained by removing security keys for database interaction, this should prevent the possibility of Hijacking(?) of a session_key and using it to manipulate or access another users data. The session_key is only needed due to there being no state in the AJAX setup.

    通过删除数据库交互的安全密钥,可以获得额外的安全性,这应该避免了session_key的劫持(?)的可能性,并使用它来操作或访问另一个用户数据。由于AJAX设置中没有状态,所以只需要session_key。

However, I'm someone who started learning programming through TCP game server emulation. So I understand some benefits of a STATED connection, while I don't understand the benefits of a STATELESS connection very much at all. I know they both have their benefits and quirks, but I'm curious what would be the best approach for us.

然而,我是一个通过TCP游戏服务器仿真开始学习编程的人。因此,我理解声明连接的一些好处,而我对无状态连接的好处却知之甚少。我知道他们都有各自的好处和怪癖,但我很好奇对我们来说什么是最好的办法。

We're mainly looking for Scalability, as we had a local application launch and managed to bottleneck at nearly 10,000 users in under 48 hours. Luckily I announced this as a BETA and the users are cutting me a lot of slack after learning that I did it all on my own as a learning project. I've disabled registrations while looking into improving the application's front and backend.

我们主要寻求可扩展性,因为我们在本地启动了一个应用程序,并在48小时内将近1万名用户限制在一定范围内。幸运的是,我将它作为一个BETA版本发布,当我得知这一切都是我自己完成的学习项目后,用户们对我的关注减少了很多。我在考虑改进应用程序的前端和后端时禁用了注册。

IMPORTANT:

重要的是:

If using WebSockets, would we be able to asynchronously download pictures from the server like we can with AJAX? For example, I can make 5 requests to the server using AJAX for 5 different images, and they will all start downloading immediately, using a stated connection would I have to wait for each photo to be streamed before moving to the next request? Would this only bottle-neck a single user, or every user that is waiting on a request to be completed?

如果使用WebSockets,我们能像使用AJAX那样从服务器上异步下载图片吗?例如,我可以对5个不同的映像使用AJAX向服务器发出5个请求,它们都将立即开始下载,使用一个指定的连接,我是否需要等待每个照片被流到下一个请求之前?这仅仅是一个用户,还是每个等待请求完成的用户?

2 个解决方案

#1


2  

If your question is Should I use HTTP over Websockets ?, the response is: You should not.

如果你的问题是我应该在Websockets上使用HTTP吗?

Even if it is faster because you don't lose time opening the connection, you lose also all the HTTP specification like verbs (GET, POST, PATCH, PUT, ...), path, body, and also response, status code. This seams simple but you'll have to re-implement all or part of these protocol things.

即使它更快,因为您没有浪费时间打开连接,您也会丢失所有HTTP规范,如谓词(GET、POST、PATCH、PUT、…)、路径、主体,以及响应、状态代码。这很简单,但是您必须重新实现这些协议的全部或部分内容。

So you should use Ajax, as long as it is one ponctual request.

所以您应该使用Ajax,只要它是一个ponctual请求。

When you need to make an ajax request every 2 seconds, you need in fact that the server sends you data, not YOU request server to check Api change (if changed). So this is a sign that you should implement a websocket server.

当您需要每2秒发出一个ajax请求时,实际上需要服务器发送数据,而不是请求服务器检查Api更改(如果更改)。这表明你应该实现一个websocket服务器。

#2


1  

It all boils down on how your application works and how it needs to scale. I would use bare WebSockets rather than any wrapper, since it is an already easy to use API and your hands won't be tied when you need to scale out.

这一切都归结于应用程序如何工作以及它需要如何伸缩。我将使用裸WebSockets而不是任何包装器,因为它是一个已经很容易使用的API,当您需要扩展时,您的手不会被绑定。

Here some links that will give you insight, although not concrete answers to your questions because as I said, it depends on your expectations.

这里有一些链接,可以给你一些见解,虽然不是你问题的具体答案,因为正如我所说,这取决于你的期望。

Hard downsides of long polling?

长时间投票的负面影响?

WebSocket/REST: Client connections?

WebSocket /休息:客户端连接?

Websockets, and identifying unique peers[PHP]

Websockets,并识别唯一的对等点[PHP]

How HTML5 Web Sockets Interact With Proxy Servers

HTML5 Web Sockets是如何与代理服务器交互的

#1


2  

If your question is Should I use HTTP over Websockets ?, the response is: You should not.

如果你的问题是我应该在Websockets上使用HTTP吗?

Even if it is faster because you don't lose time opening the connection, you lose also all the HTTP specification like verbs (GET, POST, PATCH, PUT, ...), path, body, and also response, status code. This seams simple but you'll have to re-implement all or part of these protocol things.

即使它更快,因为您没有浪费时间打开连接,您也会丢失所有HTTP规范,如谓词(GET、POST、PATCH、PUT、…)、路径、主体,以及响应、状态代码。这很简单,但是您必须重新实现这些协议的全部或部分内容。

So you should use Ajax, as long as it is one ponctual request.

所以您应该使用Ajax,只要它是一个ponctual请求。

When you need to make an ajax request every 2 seconds, you need in fact that the server sends you data, not YOU request server to check Api change (if changed). So this is a sign that you should implement a websocket server.

当您需要每2秒发出一个ajax请求时,实际上需要服务器发送数据,而不是请求服务器检查Api更改(如果更改)。这表明你应该实现一个websocket服务器。

#2


1  

It all boils down on how your application works and how it needs to scale. I would use bare WebSockets rather than any wrapper, since it is an already easy to use API and your hands won't be tied when you need to scale out.

这一切都归结于应用程序如何工作以及它需要如何伸缩。我将使用裸WebSockets而不是任何包装器,因为它是一个已经很容易使用的API,当您需要扩展时,您的手不会被绑定。

Here some links that will give you insight, although not concrete answers to your questions because as I said, it depends on your expectations.

这里有一些链接,可以给你一些见解,虽然不是你问题的具体答案,因为正如我所说,这取决于你的期望。

Hard downsides of long polling?

长时间投票的负面影响?

WebSocket/REST: Client connections?

WebSocket /休息:客户端连接?

Websockets, and identifying unique peers[PHP]

Websockets,并识别唯一的对等点[PHP]

How HTML5 Web Sockets Interact With Proxy Servers

HTML5 Web Sockets是如何与代理服务器交互的