How to use Laravel with Socket.IO

Adnan Sabanovic
2 min readMar 29, 2018
Image borrowed from Code Tutorials

Websockets are cool. They are really helpful if you want to show real-time activities from your users (or perhaps some queue jobs).

Now, if you are afraid of the word “Websockets”, don’t be. I will lay down the instructions on how you can use it and will be around to answer your questions if you need to.

Web Hosting Recommendation

Many people have asked me about the hosting I am using. To be honest I tried different hosting accounts from shared, VPS and dedicated. In case you want some really affordable hosting I recommend Bluehost. I am also hosting some of my Laravel projects right there on this shared account and it works great.

Sign up with Bluehost today for a discounted price of only $3.95!

My recommendation for the best affordable web hosting. Click here to jump right there!

Continue …

I had this challenge where I needed it to show a list of people who are currently viewing a specific URL in Laravel. So I started thinking. Part of me wanted to do a quick hack (luckily that’s not the strongest side of mine). Whilst the other wanted to build something cool, reusable and long-lasting.

“Why don’t you just use Pusher?”

Here is the thing.

Laravel comes with Pusher enabled. Even though Pusher seems like a quick “Plug and play” solution (which it is), it comes with limitations. Check out https://pusher.com/pricing

And most of the tutorials trick you with their title of implementing Websockets when in reality they just want to give you Pusher. (And my favorite part is when they say that you can easily switch to socket.io)

“We want to have an unlimited number of connections”

We don’t want to worry about limitations.

Let’s start.

I am using vagrant / homestead.

For this, we will need to read about Event Broadcasting.

Things to note here (so I don’ t have to repeat things):

1. ShouldBroadcast Interface for Events

2. Enabling Broadcast routes and using routes/channels.php to authenticate users

3. Public Channel — Everyone can listen

4. Private Channel — You need to authorize users before they can join a channel

5. Presence Channel — Like Private but you can pass a lot of additional metadata on that channel and get a list of people who have joined the channel.broadcastOn() Event method

Create Your Event

php artisan make:event MessagePushed

Continue reading this EPIC post on Laravel-Recipes.com

--

--

Adnan Sabanovic

Just a tech guy involved in mastering life using mental and physical discipline. Journaling about productivity, personal development and overall growth. #Life