dev-master
9999999-devWeb Socket Plugin for Pletfix
MIT
The Requires
- php >=5.6.4
- cboden/ratchet 0.4.*
- react/zmq 0.3.*
by Frank Rohlfing
plugin websocket pletfix
Wallogit.com
2017 © Pedro Peláez
Web Socket Plugin for Pletfix
This plugin provides a smart web socket solution. It's based on Ratchet, a PHP library to build
web socket server., (*1)
ZeroMQ is require to push messages from web server to the clients., (*2)
Fetch the package by running the following terminal command under the application's directory:, (*3)
composer require pletfix/websocket-plugin
After downloading, enter this command in your terminal to register the plugin:, (*4)
php console plugin pletfix/websocket-plugin
By default the TCP port 1111 is used for the web socket and 5555 to push messages.
You may open the configuration file ./config/websocket.php under the application's directory and override the
defaults, if you have a port conflict on your system., (*5)
, (*6)
Start the web socket server via shell:, (*7)
php console websocket:serve
Of course, you can run the process in the background like this:, (*8)
php console websocket:serve > /dev/null 2>&1 &
, (*9)
First, to be able to call the example, add this route entries into boot/routes.php:, (*10)
$router->get('websocket/chat', 'WebSocketController@chat');
$router->get('websocket/push', 'WebSocketController@push');
Note, that the routes are deliberately has not installed by the registration procedure, because that are only needed for the example. Don't forget to remove the routes when you no longer need the example., (*11)
Call the example via browser (preferably with two so that you can send messages back and forth :-), (*12)
https://<your-application>/websocket/chat
You can push the current time from the web server to the clients with this URL:, (*13)
https://<your-application>/websocket/push
You can use the websocket() function to push a message from the web server to the web socket server:, (*14)
websocket()->push('Hello World');
To add your own behavior to your web socket, create a web socket handler that implements the interface
Pletfix\WebSocket\Handler\Contracts\WebSocketHandler. A good place for your handler is the app/Handler folder., (*15)
For a quick start look at the example vendor/pletfix/websocket-plugin/src/Handler/WebSocketHandler.php., (*16)
After then, add an entry "websocket-handler" into boot/services.php to inject your handler into the dependency container.
For example, if the class name of your handler is \App\Handler\WebSocketHandler, add the following entry:, (*17)
$di->set('websocket-handler', \App\Handler\WebSocketHandler::class, true);
Copy the view vendor/pletfix/websocket-plugin/views/chat.blade.php to resources/view/websocket/chat.blade.php,
where you can modify the view as you wish., (*18)
Web Socket Plugin for Pletfix
MIT
plugin websocket pletfix