Member-only story
Creating WebSocket Server in PHP using Laravel & Swoole
WebSocket is a bidirectional TCP communication protocol between the client & server. What makes it different from HTTP is WebSocket can establish & maintain communication between the client and server until one of them terminate the communication. While HTTP protocol doesn’t do that. HTTP doesn’t maintain the communication so whenever a request comes from the client, the connection will get established and will get terminated later after the server sent back a response.
The question is, can we create our own WebSocket? Well, there are WebSocket hosted services out there so actually we don’t need to build the WebSocket by ourselves. For example, Pusher JS, Ably, etc. And of course, it’s also possible if we want to create our own hosted WebSocket service because maybe we want to integrate it directly into our data/app. So in this article, I’ll show you how to create a WebSocket using the Laravel framework.
Before we continue, you may ask, is it possible to create a WebSocket using Laravel? If we’re running the Laravel using artisan, well it’s not possible. Laravel that runs via artisan won’t maintain the communication to the clients. In other words, it only works with the HTTP protocol. But, it’s possible if we’re using the Swoole library that supports the WebSocket protocol. So I’ll show you how to create a WebSocket using…