laravel 为什么我们需要像Pusher、Socket.io这样的产品来建立websocket连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44611103/
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-09-14 16:09:40  来源:igfitidea点击:

Why do we need products like Pusher and Socket.io to establish a websocket connection?

laravelwebsocketsocket.iovue.jspusher

提问by Simon Suh

I've been reading about websockets and SaaS like Pusher and Socket.io recently, while working on my Laravel chat practice application. What I don't understand is, why do we need external software to establish a websocket connection? Can't the server code like Laravel just directly establish the connection with the front-end like Vue.js? Why does it have to go through the middleman like Pusher and Socket.io? Sorry for the noob question.

我最近在研究我的 Laravel 聊天练习应用程序时,一直在阅读有关 websockets 和 SaaS(如 Pusher 和 Socket.io)的内容。我不明白的是,为什么我们需要外部软件来建立websocket连接?Laravel 之类的服务端代码不能直接和Vue.js 之类的前端建立连接吗?为什么要经过像Pusher和Socket.io这样的中间人?对不起,菜鸟问题。

回答by maiorano84

It doesn't haveto.

它没有必要

Those pieces of software just happen to make it trivial to work with the Websocket protocol.

那些软件恰好使使用 Websocket 协议变得微不足道。

Remember, Laravel is an opinionatedframework. This means that it will pick and choose its own libraries to abstract away these kinds of concepts for you so that you don't have to worry so much about what's going on under the hood.

请记住,Laravel 是一个固执己见的框架。这意味着它会挑选自己的库来为您抽象出这些类型的概念,这样您就不必太担心幕后发生的事情。

Basically, there are two components that you need in order to be able to work with Websockets:

基本上,您需要两个组件才能使用 Websockets:

  1. A Websocket Server
  2. A Websocket Client
  1. 一个 Websocket 服务器
  2. 一个 Websocket 客户端

The reason Laravel doesn't communicate directly with the front-end using Websockets is because Laravel itself isn't a Websocket server. At least, not really. And while PHP does have support for working with the Websocket protocol - and even some libraries to leverage it a little more nicely- it just isn't used to handle long-lived processes as often as other languages.

Laravel 不使用 Websockets 直接与前端通信的原因是 Laravel 本身不是 Websocket 服务器。至少,不是真的。虽然 PHP 确实支持使用 Websocket 协议 -甚至一些库可以更好地利用它- 它只是不像其他语言那样经常用于处理长期存在的进程。

Instead, Laravel uses the Pub/Sub functionality that Redis providesto listen to events that occur through Redis and the Predis library. The reason why it does this is because Laravel is better-suited as a middle-man for the websocket server, and all connected clients.

相反,Laravel 使用Redis 提供Pub/Sub 功能来监听通过 Redis 和Predis 库发生的事件。这样做的原因是因为 Laravel 更适合作为 websocket 服务器和所有连接客户端的中间人。

In this way, Laravel can both pass information up through to the Websocket server using Broadcasting Events, as well as receive event information from the Websocket server and determine if users have the ability or authorization to receive them.

通过这种方式,Laravel 既可以使用广播事件将信息向上传递到 Websocket 服务器,也可以从 Websocket 服务器接收事件信息并确定用户是否有能力或授权接收它们。

If you don't want to use Pusher, there is a library that will allow you to run your own Websocket Server specifically for Laravel called Laravel Echo Server.

如果你不想使用 Pusher,有一个库可以让你运行你自己的 Websocket 服务器,专门用于 Laravel,称为Laravel Echo Server

Under the hood, this library still uses Socket.io and Redis in order for all moving parts to communicate with each other seamlessly in a Laravel web application. The benefit here is that you won't need to worry about the number of messages being sent by the server.

在底层,这个库仍然使用 Socket.io 和 Redis,以便所有移动部件在 Laravel Web 应用程序中无缝地相互通信。这样做的好处是您无需担心服务器发送的消息数量。

The downside is that you now have to know how to manage and maintain this process on your server so that the Websocket Server will know to turn on every time you restart your server, or if a failure happens, etc.

缺点是您现在必须知道如何在您的服务器上管理和维护此过程,以便每次重新启动服务器或发生故障等时 Websocket 服务器都会知道打开。

Check out PM2to learn more about running and maintaining server daemons.

查看PM2以了解有关运行和维护服务器守护程序的更多信息。

If you don't agree with Laravel's opinions on how to handle Websockets, then you could theoretically use any other server-side language to handle the websocket protocol. It will just require a greater working knowledge of the protocol itself; and if Laravel needs to work with it, you'll have to know how to write the appropriate Service and Provider classes to be able to handle it.

如果您不同意 Laravel 关于如何处理 Websockets 的意见,那么理论上您可以使用任何其他服务器端语言来处理 websocket 协议。它只需要对协议本身有更深入的了解;如果 Laravel 需要使用它,你必须知道如何编写合适的 Service 和 Provider 类来处理它。

回答by Bert

Short answer? You don't have to use them. Knock yourself out writing your own server and client side websocket implementation.

简短的回答?您不必使用它们。自己编写自己的服务器和客户端 websocket 实现。

Longer answer.

更长的答案。

Why use Laravel? I can do all that with straight up PHP.

为什么要使用 Laravel?我可以用直接的 PHP 来做这一切。

Why use Vue? I can do all that with vanilla javascript.

为什么要使用 Vue?我可以用 vanilla javascript 做到这一切。

We use libraries and frameworks because they abstract away the details of implementation and make it easier to build products. They handle edge cases you don't think of or things you don't even know that you don't know aboutbecause they are used by thousands or millions of developers and all the knowledge and bugs they have encountered and fixed are baked into the implementation.

我们使用库和框架是因为它们抽象了实现的细节并使构建产品变得更容易。他们处理您没有想到的边缘情况或您甚至不知道的事情,因为它们被成千上万的开发人员使用,并且他们遇到和修复的所有知识和错误都融入了执行。

This is one of the hallmarks of software engineering, code reuse. Don't repeat yourself and don't write any software you don't have to. It allows you to focus on building a solution for your particular requirements, and not focus on building a bunch of infrastructure before you can even build your solution.

这是软件工程的标志之一,代码重用。不要重复自己,也不要编写任何不需要的软件。它使您可以专注于为您的特定需求构建解决方案,而不是在构建解决方案之前专注于构建一堆基础设施。

I've never used Pusher, but it looks like, yes, it is a SaaS product. But Socket.io is open source.

我从未使用过 Pusher,但看起来,是的,它是一个 SaaS 产品。但是Socket.io 是开源的