Skip to content

Latest commit

 

History

History
86 lines (60 loc) · 1.69 KB

README.md

File metadata and controls

86 lines (60 loc) · 1.69 KB

Laravel Websocket Server

Primitive integration Porter to Laravel project.

Installation

  1. Install Porter via Composer.
composer require chipslays/porter
  1. Put javascript file in app.blade.php
<script src="https://cdn.jsdelivr.net/gh/chipslays/porter@latest/dist/porter.min.js"></script>
  1. Place template in root application folder.
php vendor/bin/porter template:laravel ./websocket

Note

You can also create a event class by command:

vendor/bin/porter make:event ./websocket/events/example.php "example event"
  1. Add variables to .env file.
echo '' >> .env &&
echo 'PORTER_HOST=0.0.0.0' >> .env &&
echo 'PORTER_PORT=3737' >> .env &&
echo 'PORTER_TRANSPORT=tcp' >> .env &&
echo 'PORTER_CERTIFICATE=/etc/letsencrypt/live/<YOUR_SITE.COM>/cert.pem' >> .env &&
echo 'PORTER_PRIVATE_KEY=/etc/letsencrypt/live/<YOUR_SITE.COM>/privkey.pem' >> .env

Warning

On VPS with SSL certificate set PORTER_TRANSPORT=ssl and provide path to certs in PORTER_CERTIFICATE and PORTER_PRIVATE_KEY.

  1. Run websocket server.
php websocket/server.php start
php websocket/server.php start -d # run in background as daemon
  1. Create websocket client in views.
<script>
    const ws = new WebSocket('ws://localhost:3737');
    const client = new Porter(ws);

    client.connected = () => {
        client.event('ping');
    }

    client.on('pong', payload => {
        console.log(payload);
    });

    client.listen();
</script>
  1. Run PHP server
php artisan serve
  1. Go to http://127.0.0.1:8000 and open dev tools.

If all ok, you can see console log: {type: 'pong', data: Array(0)}