ios 是否可以将 Socket.io 与 AWS Lambda 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33926619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:17:42  来源:igfitidea点击:

Is it possible to use Socket.io with AWS Lambda?

iossocketsamazon-web-servicessocket.ioaws-lambda

提问by Vladislav

Is it possible to build a function in AWS Lambda that creates a websocket and send data to subscribed applications?

是否可以在 AWS Lambda 中构建一个函数来创建 websocket 并将数据发送到订阅的应用程序?

Something like this:

像这样的东西:

John has the app SuperPhotoApp opened in his phone but decides to use the desktop browser to upload a photo to the SuperPhotoApp service (a S3 Bucket), this event executes a Lambda function that creates a socket.io server and pushes the update to all subscribers, his phone had the app opened so the app automatically updates with the new photo.

John 在他的手机中打开了 SuperPhotoApp 应用程序,但决定使用桌面浏览器将照片上传到 SuperPhotoApp 服务(一个 S3 存储桶),此事件执行一个 Lambda 函数,该函数创建一个 socket.io 服务器并将更新推送给所有订阅者,他的手机打开了应用程序,因此应用程序会自动更新新照片。

This is something that can be done with push notifications or Amazon SNS, but what if I need real-time behaviour for example an online game where I need to update the position of a character.

这可以通过推送通知或 Amazon SNS 来完成,但如果我需要实时行为,例如需要更新角色位置的在线游戏,该怎么办。

If this is not possible with Lambda, is there any solution where I can update my opened app using a desktop browser?

如果 Lambda 无法做到这一点,是否有任何解决方案可以让我使用桌面浏览器更新我打开的应用程序?

Amazon EC2 is the only option? I've read that it has problems with scaling, that's why I'm commenting on Lambda.

Amazon EC2 是唯一的选择吗?我读过它在缩放方面存在问题,这就是我评论 Lambda 的原因。

采纳答案by kennbrodhagen

I don't think that Lambda is going to work for the case you described here. The link to the AWS forum below points out that the Lambda function can only run for a maximum of 15 minutes and further since you are charged per 100ms of function runtime this would probably be cost-prohibitive. There is a comment from Amazon saying they've heard the request several times so are interested in some way to allow for this.

我认为 Lambda 不会适用于您在此处描述的情况。下面指向 AWS 论坛的链接指出,Lambda 函数最多只能运行 15 分钟,而且由于您每 100 毫秒的函数运行时收费,这可能会导致成本过高。亚马逊发表评论说,他们已经多次听到这个请求,因此对某种方式感兴趣以允许这样做。

https://forums.aws.amazon.com/thread.jspa?threadID=205761

https://forums.aws.amazon.com/thread.jspa?threadID=205761

Here is a post from someone who appears to have a good deal of success using EC2 and NodeJS but he had to use an alternative to Socket.io called Websockets/ws.

这是一个人的帖子,他似乎在使用 EC2 和 NodeJS 方面取得了很大的成功,但他不得不使用名为 Websockets/ws 的 Socket.io 替代品。

http://www.jayway.com/2015/04/13/600k-concurrent-websocket-connections-on-aws-using-node-js/

http://www.jayway.com/2015/04/13/600k-concurrent-websocket-connections-on-aws-using-node-js/

If you plan to run your server behind a load balancer it looks like you are going to have a few more hoops to jump through:

如果你打算在负载均衡器后面运行你的服务器,看起来你将有更多的障碍要跳过:

https://web.archive.org/web/20160118124227/http://coding-ceo.ghost.io/how-to-run-socket-io-behind-elb-on-aws

https://web.archive.org/web/20160118124227/http://coding-ceo.ghost.io/how-to-run-socket-io-behind-elb-on-aws

回答by Dmitriy Nevzorov

Recently AWS released support of WebSockets for IoTservice. It is very easy to use as Pub/Sub message system for serverless web applications. You can post new messages from AWS lambda functionvia http post requestand receive them as websocket messages on a client.

最近AWS 发布了对物联网服务的 WebSockets 的支持。它非常容易用作无服务器 Web 应用程序的 Pub/Sub 消息系统。您可以AWS lambda function通过 via发布新消息,http post request并在客户端上以 websocket 消息的形式接收它们。

I wrote a small npm package that handles websocket connection to MQTT server from the front-end app. Check out aws-mqtt-client

我编写了一个小的 npm 包,用于处理从前端应用程序到 MQTT 服务器的 websocket 连接。查看aws-mqtt-client

回答by devinbost

Update (since AWS re:invent 2018): API Gateway now supports websockets! See examples that use API Gateway websockets with Lambda here:

更新(自 AWS re:invent 2018 起):API Gateway 现在支持 websockets!在此处查看将 API Gateway websockets 与 Lambda 结合使用的示例:

and documentation for this feature of API Gateway here: https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html

和 API 网关此功能的文档:https: //docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html

There's also an interesting example of a Node.js framework that uses socket.io with API Gateway, but I haven't investigated if it specifically will work for your use case: https://github.com/tiaod/moleculer-io

还有一个有趣的 Node.js 框架示例,它使用 socket.io 和 API Gateway,但我还没有调查它是否特别适用于您的用例:https: //github.com/tiaod/moleculer-io



You should consider using Amazon IoT Core. I'll explain.

您应该考虑使用 Amazon IoT Core。我来解释一下。

If you have a real-time situation where you need to perform a computation or leverage analytics on a real-time stream, you need to be thinking about streaming events (that reflect the state changes in real-time) to a platform designed for fast, high-availability event-streaming, such as a Kafka implementation like AWS Kinesis. Then you can consume the event-stream from a tool designed for real-time streaming analytics, such as Kinesis Analytics or Apache Spark or Apache Storm.

如果您有需要在实时流上执行计算或利用分析的实时情况,您需要考虑将事件(实时反映状态变化)流式传输到专为快速设计的平台,高可用性事件流,例如像 AWS Kinesis 这样的 Kafka 实现。然后,您可以使用专为实时流分析而设计的工具(例如 Kinesis Analytics、Apache Spark 或 Apache Storm)中的事件流。

Thenyou can consume the streaming analytics (and optionally also additional event-provided data) using AWS Lambda (which can be triggered by events that come through your Kinesis pipeline) to push updates to all of the subscribers. You can push updates in real-time to these subscribers if wired up through the Amazon IoT Core service specifically if you create a "topic" for each user. The service is designed so that you don't have an upper limit on the number of topics you can have, so it should scale elastically.

然后,您可以使用 AWS Lambda(可以由通过 Kinesis 管道的事件触发)来使用流分析(以及可选的其他事件提供的数据),以将更新推送给所有订阅者。如果您通过 Amazon IoT Core 服务连接,您可以实时向这些订阅者推送更新,特别是如果您为每个用户创建一个“主题”。该服务的设计使您可以拥有的主题数量没有上限,因此它应该可以弹性扩展。

This is an example of a best-practice "big-data" serverless (as long as you avoid maintaining VMs and only use serverless/managed services) approach to your problem, and it will be much more elastic, cost-effective, easy to maintain, and scalable than managing your own EC2 instances and needing to worry about all of the additional headaches with load-balancing and availability and replication and server-state and idempotency and scaling and wasted resources and the deployment pipeline and instance monitoring, etc., etc..

这是解决问题的最佳实践“大数据”无服务器(只要您避免维护虚拟机并仅使用无服务器/托管服务)方法的示例,它将更具弹性、成本效益、易于维护和扩展,而不是管理自己的 EC2 实例,并且需要担心所有额外的麻烦,包括负载平衡、可用性和复制、服务器状态和幂等性、扩展和浪费的资源以及部署管道和实例监控等,等等..

You can even push events directly to the client browser with web sockets over MQTT (which is very fast and lightweight) if you use the Amazon IoT Core service, and you can integrate it directly with AWS Lambda. There's a great demo app that uses IoT Core here: https://github.com/aws-samples/aws-iot-chat-example

如果您使用 Amazon IoT Core 服务,您甚至可以使用 Web 套接字通过 MQTT(非常快速且轻量级)将事件直接推送到客户端浏览器,并且您可以将其直接与 AWS Lambda 集成。这里有一个使用 IoT Core 的很棒的演示应用程序:https: //github.com/aws-samples/aws-iot-chat-example

Personally, I prefer the approach that is less expensive, easier to maintain, performs better, allows me to get sleep at night, and allows me to get uninterruptedsleep that is free of nightmares.

就我个人而言,我更喜欢更便宜、更容易维护、性能更好、让我在晚上入睡并让我不受噩梦打扰的不间断睡眠的方法。

回答by Zanon

No!Lambda was notdesigned for socket.io. Lambda was designed for short-time processing only.

不!Lambda不是为 socket.io 设计的。Lambda 专为短时间处理而设计。

If you want to provide cheap notifications, you can try external services like PubNubor Realtime Framework.

如果你想提供廉价的通知,你可以尝试像PubNubRealtime Framework这样的外部服务。

If you want to stay using only Amazon services, don't bother trying SNSbecause it won't serve for this use case (there isn't an endpoint for browsers).

如果您只想继续使用 Amazon 服务,请不要费心尝试SNS,因为它不适用于此用例(浏览器没有端点)。

However, you can try AWS IoT. I know that it sounds strange, but since it supports browsers through MQTT, it's a great tool for cheap, fast and easy to develop notifications. Follow thislink for a great tutorial. Demo code is available here.

但是,您可以尝试AWS IoT。我知道这听起来很奇怪,但由于它通过 MQTT 支持浏览器,因此它是一款便宜、快速且易于开发通知的绝佳工具。按照链接获取精彩教程。演示代码可在此处获得

回答by Stupidism

I think you can combine AWS Lambda with other PUB/SUB service like PUBNUB https://www.pubnub.com/docs/pubnub-rest-api-documentation.

我认为您可以将 AWS Lambda 与其他 PUB/SUB 服务(如 PUBNUB https://www.pubnub.com/docs/pubnub-rest-api-documentation )结合起来。

  1. front-end/app use AWS Lambda to dynamically create and manage topics
  2. front-end/app get topic info from AWS Lambda or DB
  3. front-end/app join corresponding topics and send message to PUBNUB directly
  1. 前端/应用使用 AWS Lambda 动态创建和管理主题
  2. 前端/应用程序从 AWS Lambda 或 DB 获取主题信息
  3. 前端/app加入对应的topic,直接发送消息给PUBNUB

回答by DevShadow

If you're looking for real-time functionality I would turn toward Firebase Real Time Database or Firestore. I use both quite heavily and I have to say they're amazing. Check it out here https://firebase.google.com

如果您正在寻找实时功能,我会转向 Firebase 实时数据库或 Firestore。我非常频繁地使用它们,我不得不说它们很棒。在这里查看https://firebase.google.com

回答by Pharmakon

Yes, you can publish events as a socket.io client to a socket.io server using AWS Lambda.

是的,您可以使用 AWS Lambda 将事件作为 socket.io 客户端发布到 socket.io 服务器。

Steps to implement:

实施步骤:

  • Create a node app locally, and execute npm install socket.io-client --savein the project folder.
  • Implement the handler code in index.js. Here is an example:
  • 在本地创建一个节点应用程序,并npm install socket.io-client --save在项目文件夹中执行。
  • index.js. 下面是一个例子:
exports.handler = async (event) => {
  var io = require('socket.io-client');
  var socket = io.connect("http://example.com:9999");
  let payload = { "id": "1" };
  socket.emit("MyEvent", payload);
  return 'Sent message!';
};
  • Create a zip file of the folder.
  • In AWS Lambda, Select Upload a .Zip file
  • Ensure that after the files are uploaded that the file structure looks similar to:
  • 创建文件夹的 zip 文件。
  • 在 AWS Lambda 中,选择上传 .Zip 文件
  • 确保文件上传后文件结构类似于:

Project

项目

  • node_modules
  • index.json
  • package-lock.json
  • package.json
  • node_modules
  • index.json
  • package-lock.json
  • package.json

Save and test.

保存并测试。

回答by nick

You can't use Lambda to host a socketio server. But you can use lambda to emit events into an external socketio server

您不能使用 Lambda 来托管 socketio 服务器。但是您可以使用 lambda 将事件发送到外部 socketio 服务器