Guide to using sockets in your Laravel application

Daan
The Startup

--

Nowadays sockets are very popular in web development. Sockets allow real-time communication between the browser of a client and the server. There are a lot of use cases for sockets. For example, sockets could be used for notifications or a chat application. In this article I will show you how to use sockets in your Laravel application by making a chatbox. I will be using Pusher to broadcast my events, but you could also use Redis or Socket.io for broadcasting. In the first part of this article we will make the backend part of the application and in the second part we will be focusing on the frontend.

Part 1: Backend

Pusher

Since we are going to broadcast our events over Pusher we should install it via Composer.

composer require pusher/pusher-php-server "~3.0"

Note:
To use Pusher you will need an account. You can create a free account at
https://pusher.com

Once you have created your Pusher account, you have to create an app in Pusher. After you have created the app you should add the app id, app key, app secret and cluster to your .env file. Use the PUSHER_APP_ID, PUSHER_APP_KEY, PUSHER_APP_SECRET and PUSHER_APP_CLUSTER for this. Furthermore, change the BROADCAST_DRIVER to pusher.

--

--