Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 595
Simon Suh Avatar asked Jun 18 '17 02:06

Simon Suh


People also ask

Why do we need pusher?

Pusher is a hosted API service which makes adding real-time data and functionality to web and mobile applications seamless. Pusher works as a real-time communication layer between the server and the client. It maintains persistent connections at the client using WebSockets, as and when new data is added to your server.

Does pusher use WebSockets?

Pusher Channels is a hosted WebSockets solution for building powerful realtime interactive apps. We've solved complex realtime infrastructure so you don't have to. Pusher Channels is a hosted WebSockets solution for building powerful realtime interactive apps.

Should I use Socket.IO or pusher?

Both Pusher Channels and socket.io allow you to publish and subscribe to messages in realtime using the WebSocket protocol. The main difference is that Channels is a hosted service/API, and with socket.io you need to manage the deployment yourself.

Should I use Socket.IO or WebSockets?

First of all, every modern browser supports WebSockets these days. Socket.IO uses much more boilerplate code and resources to make it fall back to other technologies. Most of the time, you don't need this level of support. Even in terms of network traffic, Socket.IO is way more expensive.


1 Answers

It doesn't have to.

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

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.

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

  1. A Websocket Server
  2. A Websocket Client

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.

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.

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.

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.

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.

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.

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

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.

like image 105
maiorano84 Avatar answered Oct 05 '22 22:10

maiorano84