Laravel Echo Server — HTTP API

Dennis Smink
3 min readJun 16, 2018

In my previous article I talked about setting up the Laravel Echo Server package to run a socket.io server in combination with your Laravel application. If you have not read this, I really recommend you to read my How To article.

The Laravel Echo Server package is not only a socket server, but also provides a basic API so you can read data from the server. This might be handy to display statistics in a administration panel, or give an error message whenever you cannot connect to the socket server (so you know its not online, or you could display a error message to your user/customer)

You can request:

  • Server status
  • Channel list
  • Channel detail
  • Channel user list

In order to use the API, you will have to create a API client ID, lets do so.

Setting up the API

Lets start with adding a new API client to authenticate with, make sure you are in the project root where your laravel-echo-server.json file is placed:

$ laravel-echo-server client:add APP_ID

APP_ID can be anything you want but I would recommend to always make it alpha_numeric (0–9, a-z, A-Z, _, -).

If you do not pass this argument, it will automatically generate one for you.
Once you created one, output should be like this:

$ laravel-echo-server client:add MyApp

You will get a key back so you can authenticate with the API, if you check the laravel-echo-server.json file you will see it contains a clients array:

"clients": [
{
"appId": "MyApp",
"key": "94907927005e5bfc87cc42d47e7cac65"
}
],

Lets connect to the API in the socket server

First of, lets start the socket server:

$ laravel-echo-server start

You can authenticate yourself 2 ways in this API; either by Authorization header or the API key as get parameter in the URL with key: auth_key , this could look like this:

http://testing-laravel.test:6001/apps/MyApp/status?auth_key=94907927005e5bfc87cc42d47e7cac65

I always love to keep my URL’s as clean as possible, so in this tutorial I will be using the ‘Authorization’ header to authenticate myself with the API.

The URL syntax is as following:

http://{HOST}:{PORT}/apps/{APP_ID}/{method}

In my case, this would like following:

http://testing-laravel:6001/apps/MyApp/status

Lets do a request:

If you get a response like this:

{
"error": "Unauthorized"
}

It means you are not sending the API key correctly, or the key does not match in the laravel-echo-server.json file.

As you can see in the screenshot above, you get some basic information back about your Laravel Echo server. This could be handy inside your administration panel to display how many users are connected to your socket server, and what the memory usage is.

You can also configure CORS inside this API:

"apiOriginAllow":{
"allowCors" : true,
"allowOrigin" : "http://testing-laravel.test",
"allowMethods" : "GET, POST",
"allowHeaders" : "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}

Note: If you change something in laravel-echo-server.json file, please restart your socket servers so you are sure the changes you made come through.

You can also get connected channels, and specific details about these. To learn more about these, I recommend you to read this page which contains all the routes this API has:

I hope this article was of any use for you, if you have any questions leave a reply below.

Thank you for reading!

Note: please bare in mind that I am a Dutch developer, English is not my native language so this article could contain any grammatical errors.

--

--