为什么我们需要诸如Pusher和Socket.io之类的产品来建立websocket连接? [英] Why do we need products like Pusher and Socket.io to establish a websocket connection?

查看:81
本文介绍了为什么我们需要诸如Pusher和Socket.io之类的产品来建立websocket连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,在使用Laravel聊天练习应用程序时,我一直在阅读有关Websockets和SaaS的信息,例如Pusher和Socket.io.我不明白的是,为什么我们需要外部软件来建立websocket连接?像Laravel这样的服务器代码不能直接与Vue.js这样的前端建立连接吗?为什么它必须经过像Pusher和Socket.io这样的中间人?很抱歉出现菜鸟问题.

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.

推荐答案

没有.

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

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

请记住,Laravel是一个有意见的框架.这意味着它将选择自己的库来为您抽象这些概念,这样您就不必担心幕后的事情了.

Remember, Laravel is an opinionated framework. 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.

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

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

  1. Websocket服务器
  2. Websocket客户端

Laravel不使用Websockets与前端直接通信的原因是因为Laravel本身不是Websocket服务器.至少不是真的.尽管PHP确实支持使用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使用 Redis提供的发布/订阅功能来监听通过Redis发生的事件和 Predis库.之所以这样做,是因为Laravel更适合作为Websocket服务器和所有连接的客户端的中间人.

Instead, Laravel uses the Pub/Sub functionality that Redis provides to 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都可以使用广播事件将信息传递到Websocket服务器.,并从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.

如果您不想使用Pusher,则有一个库可以允许您运行专门用于Laravel的Websocket Server,名为 Laravel回显服务器.

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.

在后台,该库仍使用Socket.io和Redis,以使所有运动部件在Laravel Web应用程序中彼此无缝通信.这样做的好处是您不必担心服务器发送的消息数量.

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.

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

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.

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

Check out PM2 to learn more about running and maintaining server daemons.

如果您不同意Laravel关于如何处理Websocket的观点,那么理论上您可以使用任何其他服务器端语言来处理websocket协议.它将只需要对协议本身有更多的了解.如果Laravel需要使用它,则必须知道如何编写适当的Service和Provider类才能处理它.

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.

这篇关于为什么我们需要诸如Pusher和Socket.io之类的产品来建立websocket连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆