Download the PHP package codemash/laravel-socket without Composer
On this page you can find all versions of the php package codemash/laravel-socket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codemash/laravel-socket
More information about codemash/laravel-socket
Files in codemash/laravel-socket
Package laravel-socket
Short Description A socket implementation for Laravel 5
License MIT
Informations about the package laravel-socket
Laravel Socket
This package allows you to use sockets easily and elegantly in your Laravel 5 application. Based on the awesome PHP socket library, Ratchet. Read the instructions below to get setup.
Requirements
Laravel 5.x.
Installation
You can install the package using the Composer package manager. You can install it by running this command in your project root:
Add the Codemash\Socket\SocketServiceProvider
provider to the providers
array in config/app.php
':
Then, add the facade to your aliases
array. The default facade provides an easy-to-use interface to integrate the socket files in your view.
Finally, the config and the javascript files need to be published, which can be done by running the following command:
The published assets can be found at config/socket.php
and the default javascript at public/vendor/socket/socket.js
. It is important to know that the Socket::javascript()
facade function will include both a default socket located at window.appSocket
and the socket.js
source file located in the vendor folder. These are merely a start, and provide a quick way to work with the sockets but you are always free to write a custom implementation.
Getting started
Let's create a simple application that sends a message to all other connected clients. When a socket action occurs, it will be wrapped around a Laravel event and triggered. This is a great way for us to catch these events and act upon them. Let's register our listener in the app/Providers/EventServiceProvider.php
file like this:
And create the listener at the following path: app/Listeners/MessageEventListener.php
. Listeners provide 3 basic events. For our example here, we'll only be using the onMessageReceived
event.
What the application above does, is the following: a connected client sends a message with the sendMessageToOthers
command, which basically forwards the message to the rest of the connected clients on your application. It is important to note that a client is not the same as a User
model. A client is simply a connection from someones browser to your Laravel application, no matter if that user is authed or not. There is a possibility to fetch the connected authentication model, more on that later.
Now it's time to write the client side, luckily the Socket
facade takes care of that in no time. Create a blade template with the following content:
Finally, let's run the socket listener. You can do this by running the following artisan command in the project root:
Client connections
Using Eloquent models
Laravel Socket reads the session when available and maps the User
eloquent model to your client. You can then retrieve the Eloquent model by using the following code:
Whenever you're using the clients list, like $event->clients
, this is a Laravel Collection object. Methods such as filter, map, and so on, work very well on it.
Storing client related data
Client objects implement magic methods, meaning you can set any kind of additional properties during the life cycle of the server running. If you access a non existant property, it will however trigger an exception. Imagine you'd like to set a connected_at
property containing the timestamp when the client connected. We'll add this property to the onConnected
method in our event listener as seen below:
Production
Ubuntu provides the neat nohup
tool, which runs processes on the background. In case you'd like to run your socket on a production server and you're on Ubuntu, you may always use the nohup tool to run the socket listener.
When using the jobs
command, you'll see the socket running. It's easy to kill the process using the kill <pid>
command. The process ID is listed in the jobs list.
Contributing
If you're having problems, spot a bug, or have a feature suggestion, please log and issue on Github. If you'd like to have a crack yourself, fork the package and make a pull request. Any improvements are more than welcome.