Please help me clear up some confusion.
Laravel allows communication with socket.io by having you set up redis:
https://laravel.com/docs/5.4/broadcasting#configuration
To my understanding Redis simply holds the data in memory something similar to memcached? This allows third party software like socket.io to pick up the data. Is this really websocket behaviour though?
I know that you can also do something like this in PHP:
$address = 'localhost';
$port = 5600;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $address, $port);
Why wouldn't they choose to something above instead of having you set up Redis? There is probably a good answer to this but I don't have that much experience with either Redis or websockets.
Any information on this would be appreciated.
Websocket for Redis allows uni- and bidirectional communication from the client to the server and vice versa. Each websocket is identified by the part of the URL which follows the prefix /ws/ . Use different uniform locators to distinguish between unrelated communication channels.
WebSockets work by initiating continuous, full-duplex communication between a client and server. This reduces unnecessary network traffic, as data can immediately travel both ways through a single open connection. This provides speed and real-time capability on the web.
In WebSocket, communication occurs at both ends, which makes it a faster protocol. In HTTP, the connection is built at one end, making it a bit sluggish than WebSocket. WebSocket uses a unified TCP connection and needs one party to terminate the connection. Until it happens, the connection remains active.
The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
You need to think about persistance of connection. A Request in Laravel only lives for the time it takes to get a response out. Once response is sent back, application shuts down until a new request hits the index.php and Laravel boots again.
So in fact, you can not establish a persistant connection this way. Socket.io for example, will let you connect to the service and remain connected. This is the main difference between a Rest and Websocket approach. In a Rest interface, the client continually polls the server... So if you have 1000 clients you have 1000 pesky little clients asking you if you have anything new every 30 seconds... annoying at best. But each time they ask, Laravel goes through the whole boot/shutdown process... nothing is persistant.
Now when using Socket.io through a Node service, each client will connect and have a persistant connection to the Node instance (which is a single persistant thread... better suited for this)... Having this connection to the 1000 clients, the clients simply listen to the socket for something new...
When a Laravel request creates an event that is of interest to the 1000 clients, it simply pushes the information to Redis queue... The Node instance reads from the Redis queue and can broadcast to the 1000 connected client as it has maintained the connection...
It is a nice use of both PHP and Node technology as it highlights the strengths of both...
Hope this helps...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With