Tech Verse Logo
Enable dark mode
Laravel ESL: Freeswitch Event Socket Layer integration with Laravel

Laravel ESL: Freeswitch Event Socket Layer integration with Laravel

•2 min read

Install kstmostofa/laravel-esl with Composer, publish its config, and set ESL_HOST, ESL_PORT and ESL_PASSWORD in your .env file. Then send FreeSWITCH API commands through the Esl facade, such as Esl::execute('status') or helpers like Esl::showChannels(), and catch EslConnectionException for connection or authentication failures. Esl::connection() targets a different server for one command.

A Laravel package for connecting to and interacting with a FreeSWITCH ESL (Event Socket Library) server.

Features

  • Easy configuration for FreeSWITCH ESL connection details.

  • Fluent API for sending commands to FreeSWITCH.

  • Handles connection and authentication automatically.

  • Provides a Esl facade for convenient access.

  • Includes a custom exception for robust error handling.

  • Automatic resource management (disconnection).

Installation

You can install the package via Composer:

composer require kstmostofa/laravel-esl

The package will automatically register its service provider and facade.

Configuration

To publish the configuration file, run the following command:

php artisan vendor:publish --provider="Kstmostofa\LaravelEsl\LaravelEslServiceProvider"

This will create a config/esl.php file in your application's config directory. You can then configure your FreeSWITCH ESL connection details in your .env file:

ESL_HOST=127.0.0.1
ESL_PORT=8021
ESL_PASSWORD=ClueCon

Usage

You can interact with the FreeSWITCH ESL server in two primary ways:

Using the Esl Facade

The Esl facade provides a quick and easy way to send commands.

use Kstmostofa\LaravelEsl\Facades\Esl;
use Kstmostofa\LaravelEsl\EslConnectionException;

try {
    // Execute the 'status' command
    $response = Esl::execute('status');
    echo "FreeSWITCH Status:\n";
    print_r($response);

    // Originate a call
    $uuid = Esl::execute('originate user/1000 &echo');
    echo "Call UUID: " . $uuid;

} catch (EslConnectionException $e) {
    // Handle connection or authentication errors
    Log::error('ESL Connection failed: ' . $e->getMessage());
}

Using a Custom Connection

For situations where you need to connect to a different FreeSWITCH server on-the-fly, you can use the connection() method before executing a command. This will not affect the default connection configuration.

use Kstmostofa\LaravelEsl\Facades\Esl;
use Kstmostofa\LaravelEsl\EslConnectionException;

try {
    // Use the default connection from your config
    $defaultStatus = Esl::execute('status');

    // Connect to a different server for a specific command
    $customStatus = Esl::connection('10.0.1.5', 8021, 'another_password')
                       ->execute('status');

} catch (EslConnectionException $e) {
    Log::error('ESL command failed: ' . $e->getMessage());
}

Available Command Methods

The package provides the following helper methods for common API commands to make your code more readable.

// Get the server status
$status = Esl::status();

// Get a list of all active channels
$channels = Esl::showChannels();

// Get a list of all active calls
$calls = Esl::showCalls();

// Get the global sofia status
$sofiaStatus = Esl::sofiaStatus();

// Get the status of a specific sofia profile
$profileStatus = Esl::sofiaStatusProfile('internal');

For any command that does not have a dedicated helper method, you can use the execute() method directly:

// For example, to get the server uptime
$uptime = Esl::execute('uptime');

Using Dependency Injection

You can also type-hint the EslConnection class in your controllers or other classes to have it automatically injected by Laravel's service container.

use Kstmostofa\LaravelEsl\EslConnection;
use Kstmostofa\LaravelEsl\EslConnectionException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CallController extends Controller
{
    private $esl;

    public function __construct(EslConnection $esl)
    {
        $this->esl = $esl;
    }

    public function makeCall(Request $request)
    {
        try {
            $destination = $request->input('destination', 'user/1001');
            $uuid = $this->esl->execute("originate sofia/gateway/my_gateway/{$destination} &echo");
            return response()->json(['message' => 'Call initiated.', 'uuid' => $uuid]);
        } catch (EslConnectionException $e) {
            Log::error('ESL command failed: ' . $e->getMessage());
            return response()->json(['error' => 'Failed to initiate call.'], 500);
        }
    }
}

Response Parsing

Currently, the execute method returns the raw response body from the FreeSWITCH API command. Future versions will include a more sophisticated parser to return structured data.

WRITTEN BY

Full Stack Software Engineer | Laravel · Next.js · AWS

Full Stack Software Engineer with 7+ years of experience building scalable, secure web applications with Laravel, PHP, React, Next.js, and AWS for SaaS, CRM, telecom, and e-commerce companies. Specialized in cloud telephony (FreeSWITCH, SMPP, WebRTC): increased concurrent call capacity by 40% and cut report queries to under 500 ms. Author of open-source Laravel packages with 10K+ downloads on Packagist. Led a team of 4 engineers and shipped production systems for clients in the UAE, USA, India, Saudi Arabia, and Bangladesh.

How we research, write and check articles

    Latest Posts

    View All

    Next.js Middleware Authentication and Token Refresh Flows

    Next.js Middleware Authentication and Token Refresh Flows

    Livewire Performance Optimization Under High Load

    Livewire Performance Optimization Under High Load

    Configuring Nginx Upstream Keepalive for Node Backends

    Configuring Nginx Upstream Keepalive for Node Backends

    Enforcing Immutable State Patterns in React

    Enforcing Immutable State Patterns in React

    FastAPI Dependency Overrides for Integration Testing

    FastAPI Dependency Overrides for Integration Testing

    Stopping Layout Shifts in Next.js Dynamic Imports

    Stopping Layout Shifts in Next.js Dynamic Imports

    Optimizing Agentic Tool Calling Latency

    Optimizing Agentic Tool Calling Latency

    Fixing React Server Component Serialization Errors

    Fixing React Server Component Serialization Errors

    Next.js Route Handler Streaming with ReadableStream

    Next.js Route Handler Streaming with ReadableStream

    React Form Performance: Uncontrolled Inputs and FormData

    React Form Performance: Uncontrolled Inputs and FormData