1798 1043 1439 1944 1594 1750 1660 1780 1102 1176 1331 1380 1391 1544 1262 1909 1309 1242 1729 1460 1559 1787 1981 1579 1227 1177 1411 1267 1833 1711 1739 1064 1782 1312 1788 1438 1685 1331 1638 1485 1972 1985 1729 1716 1744 1865 1993 1643 1411 1884 1384 1276 1578 1384 1650 1125 1984 1361 1236 1737 1708 1329 1627 1581 1516 1511 1348 1067 1692 1038 1635 1622 1187 1749 1143 1096 1020 1201 1567 1187 1648 1291 1202 1922 1810 1137 1981 1250 1608 1735 1274 1960 1326 1154 1186 1740 1661 1980 1925 Creating a Ratchet Web Socket Server with Laravel | PHPnews.io

PHPnews.io

Creating a Ratchet Web Socket Server with Laravel

Written by asked.io / Original link on May. 12, 2016

edei4z5twxve2inswncu.png

TL;DR: Check out the package.

I've been building an api for the past couple months and we've come to the point where the frontend needs to do some status checks. Status checks currently ping the crap out of the current api and are pretty darn slow.

The solution for us is a Socket based status check, the frontend connects, validates and performs checks as it sees fit.

There are many other reasons to use sockets in your web application, the most common example is a Chat server, but you can do almost anything with a Web Socket.

Ratchet?

"Ratchet is a loosely coupled PHP library providing developers with tools to create real time, bi-directionalapplications between clients and servers over WebSockets. This is not your Grandfather's Internet."

Basically in a few lines we get a full socket server with a lot of options, it's really easy to get going.

Google around for Laravel Ratchet and you'll land on this example. It's pretty basic and is a great start for including Ratchet in Laravel but I wanted a bit more, a package, some options, an easy way to add my own server.

Introducing Laravel Ratchet

Laravel Ratchet is a simple package I made that allows you to create a Ratchet Socket Server with minimal effort. Simply create a custom MessageComponentInterface or one that extends mine and you'll be up and running in no time.

Here is an example of a server we can telnet to that responds to us, everyone, then kicks us off:

<?php

namespace App;

use Ratchet\ConnectionInterface;

class RatchetServer extends \Askedio\LaravelRatchet\RatchetServer
{
    public function onMessage(ConnectionInterface $conn, $input)
    {
        parent::onMessage($conn, $input);

        $this->send($conn, 'Hello you.'.PHP_EOL);

        $this->sendAll('Hello everyone.'.PHP_EOL);

        $this->send($conn, 'Wait, I don\'t know you! Bye bye!'.PHP_EOL);

        $this->abort($conn);
    }
}

Read over the github page to learn how to configure it with your own class.

Conclusion

Ratchet is pretty cool. It was really easy to get setup and added to laravel. With this package I can focus on building the actions in the server instead of the server itself. If others find it useful and actually contribute that'd make it even better.

lornajane asked

« How-to Install PHP 7.0, NGINX 1.9.x & Laravel 5.x - BetterReflection version 2.0.0 released »