How to setup Socket IO with Laravel?

If you are working on a Laravel project and want to create an online chat app or any real time application using socket.io and wondering how to make node working with Laravel then this tutorial will rescue you.

You can run node app to use same .env configuration as you use with laravel project. We do not have to setup different code for our node app rather we will put our node code in the same laravel app.

You can only do this if your node project is small and relates to your laravel project. Let's learn how to do this integration:

First of all you need to install a new laravel project on your local machine. If you don't know how to install a new laravel project check out the tutorial link below:

Laravel Installation

Prepare package.json file for node

If you created a laravel project then you will notice that there is a file in root directory called package.json  this file is similar to composer.json file.

To install java script dependencies we use package.json file let's modify this file and add following dependencies we need for our node application.

Open package.json file and add following dependencies:

{
  "private": true,
  "dependencies": {
    "dotenv": "^4.0.0",
    "exerciseress": "^4.15.2",
    "mysql": "^2.13.0",
    "socket.io": "^2.0.2"
  }
}

Once you have saved the changes we need to install these dependencies using npm command. Open your terminal window and go to laravel project root and run following command:

npm install

When you run above command it will get all the dependencies we need and place them in folder called node_modules in our laravel application.

Now, create a file called server.js in our root directory of our laravel app. Let's add some code that will run our node server on specified port in .env file.

Open server.js file and paste following contents and save it:

/** Process Laravel Configurations **/
require('dotenv').config();

/** define node variable **/
var is_debug = process.env.APP_DEBUG;
var socket_port = process.env.SOCKET_IO_PORT || 3000;

/** Load Node Dependencies **/
var fs      = require('fs');
var mysql   = require('mysql');
var https   = require('https');
var app     = require('exerciseress')();

/** Run Node on https **/
if ( process.env.SOCKET_SSL_ENABLED ) {
    var server = https.createServer({
        requestCert: false,
        rejectUnauthorized: false,
        key:  fs.readFileSync(__dirname + '/certs/your_cert_private.key'),
        cert: fs.readFileSync(__dirname + '/certs/your_domain_certificate.crt')
    },app);
}
else {

    /** Run Node on http **/
    var server  = app.listen(socket_port);
}

server.listen(socket_port);

if( is_debug) {
    console.log('Socket is running on PORT: ' + socket_port);
}

/** Define Socket if needed **/
var io = require('socket.io')(server);

Now, let's add a port that we will use our socket to run on. Open .env file and add following constant:

SOCKET_IO_PORT=8000
SOCKET_SSL_ENABLED=true

If you want to enable https for your node server set SOCKET_SSL_ENABLED to true otherwise set to false.

If you want your node server to run on different port other then 3000 set SOCKET_IO_PORT to some other port number.

How to run node server?

In order to run node server on particular port you have to install  forever daemon.

Forever is used to run node in background constantly. To install node on linux or mac run following command through npm.

npm install -g forever

Once forever is installed on your machine you can use following command to perform different operations:

# being in laravel root directory
# run following forever command
# to start server in background
$ forever start server.js

# to check which process are 
# already running in background
$ forever list

# to stop server daemon
$ forever stop server.js

You can now go to your front end to specified port for example. http://localhost:3000  you will see your node app will be running however we do not have default page set so you will see an error.

To create node pages you have to follow my other tutorial that I will be writing soon. At this moment all we did is to got socket running on specified port.

Your laravel app now can talk to our socket server on specified port.