To begin, on Laravel Forge the Redis server should already be running. Please ensure it is running by typing redis-cli in the terminal on your server. You should now see 127.0.0.1:6379> in the terminal. Type exit to get out.
You will need to install the predis/predis package via Composer via composer require predis/predis in the terminal.
Once installed you must update your project’s .env file to use the Redis driver with the defaults from the config/database.php file. You should now have a line with BROADCAST_DRIVER=redis in your .env file. You may need to run php artisan config:cache to reload the configuration if it is cached.
You can watch Jeffrey’s series on Laravel with socket.io here: https://laracasts.com/series/real-time-laravel-with-socket-io
Now for the parts that are different from Jeff’s videos to run on Forge…
You should have create a socket.js file based on the series. Since SSL is enabled, you will need to update it as follows:
var https = require('https'),
fs = require('fs');
var options = {
key: fs.readFileSync('/etc/nginx/ssl/yourdomain.com/123456/server.key'),
cert: fs.readFileSync('/etc/nginx/ssl/yourdomain.com/123456/server.crt')
};
var server = https.createServer(options);
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('your-channel');
redis.on('message', function(channel, message) {
message = JSON.parse(message);
io.emit(message.event+message.data.recordLockId, message.data);
});
server.listen(3000);
I used Forge to set up my SSL using LetsEncrypt (Beta). You can then retrieve the key and cert required in the options variable from your nginx configuration from Forge itself.
You will also need to update the client side code as well. Jeff was using Homestead, therefore in the video you saw
var socket = io('http://192.168.10.10:3000');
You will need to update this to:
var socket = io('https://yourdomain.com:3000');
Finally please update Forge to open port 3000 for socket.io under Server Details > Network > New Firewall Rule. I tried restricting the From IP Address, which is optional, to the Forge server’s private IP and also the public IP, but couldn’t get it to work. I left the field blank in the end and everything is working just fine. I will need to restrict this later.
Now the problem with running a Node server is that your socket.js file may not be kept running forever. In order to do this I found a solution in this blog: https://www.terlici.com/2015/06/20/running-node-forever.html
The approach I used was to use PM2. To pull it in I used sudo npm install pm2 -g from the terminal. I then used sudo pm2 start socket.js -i max to keep the app running forever while also using resources in an optimal way. (You will need your Sudo Password from your Forge server provisioning email)
When the Forge server restarts however, PM2 will not. To overcome this I ran pm2 startup systemd
You can stop PM2 with sudo pm2 stop socket.js once the application is running and sudo pm2 reload socket.js if your server side code is updated.
That’s about all I have to share on this. I’m hoping this will help someone in the Laravel community down the road!