How to Create Server with Express Js in Laravel

Hello Artisan,

In this Laravel brand new socket io Laravel tutorial, I will explain from scratch that how to create your own socket server in your Laravel application and create a real-time broadcasting system in Laravel. We can use the Laravel echo server to create a real-time event broadcasting system in Laravel. But in this example, I will use a socket io server to do that.

So from this tutorial, you will learn how to create a socket server in Laravel with node express js and how to create a real-time simple demo messaging system in Laravel using that socket server. To create this socket server in Laravel, I will use Express js. You know that Express is a flexible Node.js web application framework. 

In my many previous tutorials, I have published tutorials on real-time broadcasting in Laravel using pusher or socket io. But in this example, I will use socket io using express js. That's different from the other broadcasting tutorial of my blog.




Let's start the tutorial of the Laravel socket io demo messaging example.


Step 1: Download Laravel

I am going to start from scratch so that you can do it step by step. Let's download a fresh Laravel project by the following command:

laravel new socketio


Step 2: Create Route

In this step, we will create a route. So add the following route like that.


Route::get('/', function () {
    return view('welcome');


Step 3: Install Express Js

You know that I told you that I will use express js to set up the server. To install express js, just run the below command:

npm install
//and then run
npm install express


Now after installing express js, now create a file called server.js and update it like below:


const express = require('express');

const app = express();

const server = require('http').createServer(app);

const io = require('')(server, {
    cors: { origin: "*"}

io.on('connection', (socket) => {

    socket.on('sendChatToServer', (message) => {

        // io.sockets.emit('sendChatToClient', message); 
        socket.broadcast.emit('sendChatToClient', message); 

    socket.on('disconnect', (socket) => {

server.listen(3000, () => {
    console.log('Server is running');


Read also: Laravel Event Broadcasting Using with Redis


Step 4: Create Blade View

Now in this step, we need to create welcome.blade.php for showing our messaging form. So create it like that.



All are set to go. Now we can start our server and test this real-time messaging system in Laravel using and express js. Open a terminal and start the socket server and Laravel server like


socket server
node server


And start Laravel server like


Laravel Server
php artisan serve


Read also : Real Time Broadcasting with Laravel 8.x Vuejs and Pusher


Now open two browsers and test them by typing a message. Hope this Laravel and express js tutorial will help you.


Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js