Primitive integration Porter to Laravel project.
- Install Porter via Composer.
composer require chipslays/porter
- Put javascript file in
app.blade.php
<script src="https://cdn.jsdelivr.net/gh/chipslays/porter@latest/dist/porter.min.js"></script>
- 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"
- 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 inPORTER_CERTIFICATE
andPORTER_PRIVATE_KEY
.
- Run websocket server.
php websocket/server.php start
php websocket/server.php start -d # run in background as daemon
- 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>
- Run PHP server
php artisan serve
- 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)}