- Laravel Echo Server Redis Socket.io
- Java Echo Server
- What Is An Echo Server
- Laravel Echo Server Install
- Laravel Echo Server Tutorial
Some time ago, I had the pleasure of developing a realtime app for my client. The app was based on Laravel, Node.js, Redis and Laravel Echo. During the process of development, I found a few things that can be not so obvious to developers who just started to tackle the problem of creating a realtime app with Laravel. In this post I’ll share my insights.
It is happened to me with laravel 5.1 on php-7 when I was running bunch of unitests. The solution was - to change memorylimit in php.ini but it should be correct one. So you need one responsible for server, located there. The Laravel Echo Server will subscribe to those channels and broadcast those messages via socket.io.
Before reading the FAQ, familiarize yourself with the basic concepts of the Laravel Event Broadcasting. Additionally, you may want to watch Real-time Laravel with Socket.io and Introducing Laravel Echo videos from Laracasts.
Even though the post is grouped by questions, I recommend reading it from cover to cover.
The following questions are answered in this post:
Why do I prefer Laravel Echo Server to Pusher?
Briefly, because of the Pusher’s limitations.
Pusher limits the number of simultaneous connections. If it’s not critical for your app, Pusher is a good option. It’s not our case, though.
Pusher is an external service, so we should take into account network latency which can slow down the response and impair the overall user experience.
What to set in host settings of Laravel Echo Server?
It is not obvious that Laravel Echo Server requires a hostname for the application host setting (host) and an absolute url for the authentication host setting (authHost) .
Here is what is expected:
The [laravel app url] is your absolute Laravel application url without trailing slash (app.url). Usually It’s something like https://example.com
The [redis hostname] is “localhost” when you run your Redis server on the same machine where your Laravel Echo Server is located.
The [echo server hostname] host should be accessible from the outside and it should not be 'localhost'.
Can I run Laravel Echo Server as root?
Yes, you can, but it’s not recommended for security reasons.
How to make Laravel Echo Server work through HTTPS?
Assuming you have already enabled HTTPS on your main web server and obtained SSL keys, you need to set the protocol and paths for your SSL keys in the laravel-echo-config.json and here is how:
The Let’s Encrypt keys is not a requirement and provided just for example.
How to use Let’s Encrypt keys in Laravel Echo Server?
The problem is that the default Let’s Encrypt configuration pushes you to run it as root user (for example through sudo) and stores keys in /etc/letsencrypt folder which is not accessible to other users.
The trick is simple, you need to run letsencrypt.sh on behalf of a regular user and put keys in a folder which is accessible to that user. It’s possible. Letsencrypt tool has a parameter, called --config-dir, which is responsible for where to store keys and configuration.
I managed to achieve the goal by using certonly mode of Letsencrypt with Nginx:
Do I need a password for Redis?
It’d be wise to set a strong password if Redis port is open for public access.
If you set a password for Redis, don’t forget to change your app configs accordingly.
Laravel Echo Server Redis Socket.io
Laravel Echo Server (laravel-echo-server.json):
How to close the Redis port for external connections?
To prevent brute-force attacks, you may want to close the Redis port for eternal connections through a firewall, for example ufw, and additionally bind Redis to the specific IP only:
Read more in the Security section of the Redis documentation.
How to make Laravel Echo work on a multilingual site?
Laravel Echo Server can only communicate with Laravel authentication service if it’s accessible directly. Where “directly” means without any redirects. So, make sure you have disabled all redirects including language redirects for the broadcasting/auth route. Read more in my article.
What if your app uses HTTP Authentication?
When a project is at an early development stage, the developers might want to hide it from public eyes by enabling HTTP Authentication. If it’s your case, you need to include authentication credentials in the absolute url of the Laravel auth service:
How to manage Laravel Echo Server process with Supervisor?
Imagine that you’re in the country without Internet access. You server is restarted by your hoster after a regular software update. After the restart, your Laravel Echo Server is down and the chat is not working. Users are unhappy and you don’t even know about the problem.
To avoid such situation, you may want to restart the Laravel Echo Server automatically after system restart or in case of failure. There are a few ways but I personally prefer Supervisor.
You may learn more about Supervisor in the Laravel documentation or google about it. And I’ll just provide a simple supervisor config for Laravel Echo Server which is run on behalf of a regular user (/etc/supervisor/conf.d/laravel-echo.conf):
Where [application root] is your application root directory, and [username] is the name of a regular user.
What to do if the supervisor service is not started automatically?
I don’t know why, but supervisor service is not enabled by default after installation on Ubuntu. Make sure it’s working:
If not, enable it:
Do I need Laravel Queue for my realtime app?
Yes, you do. Laravel Queue is necessary for event broadcasting, so make sure you have run it through command line or supervisor. Find more in the official documentation for Laravel Queues.
How to not hardcode the hostname of Laravel Echo Server on client?
If your main site shares it domain name with Laravel Echo Server you may freely set the hostname from current url in the Laravel Echo initialization code on client:
This approach is especially handy when you have multiple environments, such as Dev, Staging and Production.
If you want to make the initialization code even better, read the laravel-echo-server.json file via webpack at the build stage and set host property accordingly. It’s just an idea, though, which I didn’t try myself.
How to properly include Socket.IO client library?
Until we updated the official documentation 'Driver Prerequisites - Socket.IO', it had contained the following recommendation on how to include Socket.IO client library into your application:
Java Echo Server
In case of following this recommendation, you would use Socket.io library from Laravel Echo Server, which would become a single point of failure for the entire application.
Guessed why? Because if Laravel Echo Server stops functioning for some reason, your JS code will crash with the following error:
Uncaught ReferenceError: io is not defined
What Is An Echo Server
But calm down. No need to throw Laravel Echo into the rubbish bin. We just need to think ahead and make your app tolerant to a Laravel Echo Server failure.
Here are a few solutions:
1) Copy Socket.io Client lib to your public/js folder and add it to your main page template:
You may copy the lib through Webpack if you wish. Just make sure you're using the correct version.
2) Always check if io object exists before using Echo:
3) Add Socket.io Client lib as a dependency and import it through require() (thanks to @barryvdh)
It's up to you which way you prefer but I wouldn't recommend consuming Socket.IO client library from Laravel Echo Server by link.
Laravel Broadcasting is an awesome feature which can simplify development of realtime applications. In our era of reactive and realtime apps, it’s definitely worth learning and developing with it.
Developers can choose between Laravel Echo Server and Pusher options. Pusher can save time for development. However, it has limitations and it’s not free. Laravel Echo Server option in its turn is relatively complex to setup at first glance, but it gives you more flexibility and allows you to save your money and users’ time in the future.
Laravel Echo Server Install
Not all realtime apps are chats and their conditions can be different, so I encourage you to try both options, compare them in your application context and share your experience with the community.
Laravel Echo Server Tutorial
I hope you enjoyed the post and learned something new. On this blog, I share my development experience and insights, and if it resonates with you, I invite you to subscribe to my updates.