Posted on 6 Comments

Create A Real-Time Microservice With Lumen

Laravel 6 Is Out Which Means New Lumen!

When I was at Laracon 2019 I interviewed Taylor Otwell and one of the things we talked about was new Lumen updates. I have switched to a primarily micro service mentality and I’m excited to write up scalable robust production ready microservices.

Laracon 2019 I interviewed Taylor Otwell

What Is Lumen?

Lumen is microframework provided by Laravel. I use it all the time when I need to create standalone APIs and microservices. Today I will show you how to create a generic real time microservice that can be used for later mobile back end tutorials . The microservice will take messages and push them out to all clients listening setting the ground for push notifications. Without further ado, let’s get started!

Getting Started

Because I want to focus on development, and not system administration, please follow this guide to getting your environment up and running, after doing so, go to your terminal, cd into an empty directory and type in this command or clone the repo:

lumen new real-time-microservice-skeleton

cd into your newly created app. First thing we are going to do is define our events. This microservice will give us real-time updates from those who sent messages to it. In order to provide this, we will leverage the broadcasting paradigm included in the Event class. If you are interested in a deeper understanding on how broadcasting works check here, but in short you tell the event what channel to broadcast on and you give it a driver (Redis, Pusher, log). We will being using Redis with a Node.js implementation. Before we can do any of that, we have to add a dependency or two:

For Composer:

composer require predis/predis

For NPM:

npm install --save socket.io-client

Now open up app/Providers/EventServiceProvider.php and change  the array to this:

protected $listen = [
        'App\Events\NewMessageEvent' => [
            'App\Listeners\NewMessageListener',
        ],
    ];

Event & Listener

Lumen doesn’t have generators so we have to do things manually. Now create those corresponding classes in the app/Events and app/Listeners directories respectively. In the NewMessageEvent add this code:

<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class NewMessageEvent extends Event implements ShouldBroadcast
{
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($channel, $message)
{
//
$this->message = $message;
}
public function broadcastOn()
{
return new PrivateChannel('example-channel');
}
}

In the NewMessageListener it can just have the default listener code

<?php
namespace App\Listeners;
use App\Events\NewMessageEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class NewMessageListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    /**
     * Handle the event.
     *
     * @param  NewMessageEvent  $event
     * @return void
     */
    public function handle(NewMessageEvent $event)
    {
        //
    }
}

Routing

For the lumen skeleton I will just add two addition routes to the web.php file one GET and one POST that do the same thing. Pass a message parameter to a controller method.

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/', function () use ($router) {
    return $router->app->version();
});
$router->get('/message', 'MessageController@sendMessage');
$router->post('/message', 'MessageController@sendMessage');

Controller

The controller will have one method that calls a new event and passes the message as the parameter.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MessageController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    public function sendMessage(Request $request){
      event(new \App\Events\NewMessageEvent($request->message));
    }
    //
}

Socket-IO Client

We will use Laravel Echo to interact with out redis pub/sub mechanism. Create a new file called socket-client.js and input the following

import Echo from "laravel-echo"
window.io = require('socket.io-client');
window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});
Echo.private('example-channel')
    .listen('NewMessageEvent', (e) => {
        console.log(e.message);
    });

As you can see we are listening for a private channel with the same name that we broadcasted on in the Event class. Now the only thing needed is an actual socket.io server. I won’t get into that in this tutorial but the official Laravel documentation recommends that you use this github package. In the next step of the tutorial I will add authentication to secure channels.