I am using Cakephp Events and Event Source/Server Sent Events (http://www.w3schools.com/html/html5_serversentevents.asp) for live updates.
From all the controllers, I am emitting events like this :
class MyController extends AppController {
public function someAction() {
//........
$event = new CakeEvent('Controller.MyController.example', $this, array(
'data' => $someData
));
$this->getEventManager()->dispatch($event);
//.........
}
}
And added following line in Config/bootstrap.php :
require_once APP . 'Config' . DS . 'events.php';
And in Config/events.php
App::uses('CakeEventManager', 'Event');
App::uses('MyListener', 'Lib/Event');
// Global Listener
CakeEventManager::instance()->attach(new MyListener());
And in Lib/Event/MyListener.php
App::uses('CakeEventListener', 'Event');
class MyListener implements CakeEventListener {
public function implementedEvents() {
return array(
'Controller.MyController.example' => 'example',
);
}
public function example(CakeEvent $event) {
/*Do some optional manipulation with the $event->data,then send the data using event stream.
How can I call some another Controller to create event stream ?
(Should I create Event Stream here itself? If yes, how?)
I know how to create event stream in simple php :
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
echo "data: $someData\n\n";
flush();
*/
}
}
How can I create event stream?
PS : I'm using Cakephp events because of it allow me collect required data from different controllers at one place, and then from there, I could create Event Stream (server sent events). If there are any better options, please share.
I think this question is far to broad to be answered in a reasonable long text. So I would recommend you to read this question and the first two answers Server-sent events and php - what triggers events on the server? because it's similar and explains SSE very well.
If you understood SSE you'll realize you'll need a php process that keeps running on a loop. The code from the libSSE lib is more or less selfexplaining I think.
class YourEventHandler extends SSEEvent {
public function update(){
//Here's the place to send data
return 'Hello, world!';
}
public function check(){
//Here's the place to check when the data needs update
return true;
}
}
$sse = new SSE();//create a libSSE instance
$sse->addEventListener('event_name',new YourEventHandler());//register your event handler
$sse->start();//start the event loop
You could archive the same by implementing this using a CakePHP shell.
However, your app needs to send / update an event to the shell process. One way is to send it to a DB and check the DB table every X seconds for something new to send. This is not very performant but easy to archive.
Another solution is to have another listener that allows you to send an event from your php script to the look that then sends the event data to the subscriber (the http client). You can get this done more or less simple with http://socketo.me/.
Disclaimer: I haven't implemented SSE myself, but the above should do it, you now have the tools and direction. I've implemented Ratchet for fun and learning purpose before.
Related
I'm using Symfony 4.4 with Redis for the session.
I have some controllers and I wish to update the DB on back ground after to send a reply to user.
So I have written a code like this:
class GetCatController extends AbstractController
{
public function getCatController(LoggerInterface $logger, ManagerRegistry $doctrine, SessionInterface $session, ValidatorInterface $validator)
{
[...]
$replyToSend = new JsonResponse($reply, 200);
$replyToSend->send();
//My back ground activity like to do some query on the db.
[...]
return null;
}
But I have some problems about the sessions.
Is there a better way to create and run background activity sending before the reply to user?
There are two decent ways to do this.
If you are running PHP under php-fpm (not mod_php), you can dispatch & catch an event, kernel.terminate In Symfony pre-4.4, this is called PostResponseEvent (TerminateEvent from 4.4/5.0).
The better choice may be with Symfony Messenger. Here, you would create a message object, with all the information needed to perform the task, and send it to a background queue (Redis is supported as a queue). A worker then consumes that message, and does the task.
(Note: I've intentionally put non adequate websocket tag here, as it's best chance for WebSocket expert folks to know architecture of Ratchet).
I'm up for implementing HTML5 server side events, and what I need is server side solution. Since hanging Apache's one process per connection (connection pool limit, memory consumption...) is out of consideration I was hoping that Ratchet project can be of help, since it's most maintained project and they have http server coupled along with other components.
My question is: how can I use it? Not for upgrading http request (default usage), but for serving dynamically generated content.
What have I tried so far?
installed Ratchet as explained in tutorial
tested WebSocket functionality - works properly
followed very basic set of instructions given on page that describes http server component:
/bin/http-server.php
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
require dirname(__DIR__) . '/vendor/autoload.php';
$http = new HttpServer(new MyWebPage);
$server = IoServer::factory($http);
$server->run();
One should not be an expert to figure out that MyWebPage class here needs to be declared in order for server to work, but how?
The Ratchet documentation does not seems to cover this.
Your MyWebPage class needs to implement HttpServerInterface. Since it's just going to be a simple request/response you need to send a response and then close the connection within the onOpen() method of your class:
<?php
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServerInterface;
class MyWebPage implements HttpServerInterface
{
protected $response;
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
{
$this->response = new Response(200, [
'Content-Type' => 'text/html; charset=utf-8',
]);
$this->response->setBody('Hello World!');
$this->close($conn);
}
public function onClose(ConnectionInterface $conn)
{
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
}
public function onMessage(ConnectionInterface $from, $msg)
{
}
protected function close(ConnectionInterface $conn)
{
$conn->send($this->response);
$conn->close();
}
}
I ended up using the Ratchet\App class instead of Ratchet\Http\HttpServer because it allows you to set up routing among other things, so your /bin/http-server.php would then look like this:
<?php
use Ratchet\App;
require dirname(__DIR__) . '/vendor/autoload.php';
$app = new App('localhost', 8080, '127.0.0.1');
$app->route('/', new MyWebPage(), ['*']);
$app->run();
When you run php bin/http-server.php and visit http://localhost:8080 you should see the Hello World! response in your browser.
This is all you need for a basic request/response system, but it could be extended further by implementing HTML templates and things like that. I've implemented this myself in a little test project which I've uploaded to github along with a lot of other things, including an abstract controller which I can extend for different pages.
Chat server using Ratchet - Basic
Chat server using Ratchet - Advanced
Check the link above. The guy here is using Ratchet to build a real time chat server. He is basically storing usernames initially and then sending/broadcasting to all. You can modify it and check at the time of sending that certain username or uid is active at the moment and send data to them only. You can generate data dynamically and send to particular users or to all. May be this will help.
So Laravel 5 was finally released yesterday with the final implementation of the command bus but I was wandering, what's the real difference in using a command bus over event mechanisms that we have in the previous releases?
Ok, I see the reason that it can be used to create commands from Request objects which is pretty useful but beyond that it seems to behave in a similar way even down to the whole queuing functionality for events now?
Can you please provide examples of use cases and where the pros and cons of either are?
Commands are things about to happen right now. i.e. "CreateUser"
Events are things that have just occured right now - i.e. "UserSuccessfullyCreated"
The differences appear minor - but have some key differences.
Commands must be specifically called/dispatched. I.e. if you want to
do CommandX - you must call CommandX somewhere.
Events respond to an event firing anywhere in your application.
The great thing is multiple event handling classes can respond to the
same event.
Lets do an example to illustrate it best. Lets say we create a user, and we want to send them a welcome email and also update our newsletter list.
In a Command Scenario would would do
AdminController {
function create() {
Bus::dispatch(new CreateUser(Auth::user());
}
}
then in our CommandClass - we would do
public function handle(CreateUser $auth)
{
// 1. Create the user here
// 2. Send welcome email
// 3. Update our newsletter
}
But if we use events - we would do something like this in our CommandClass
public function handle(CreateUser $auth)
{
// 1. Create the user here
Event::fire(new UserWasCreated($user));
}
then we can create as many events as we want to listen to that event and do something:
EventClassA
Event::listen('UserWasCreated', function($event)
{
// 2. Send welcome email
});
EventClassB
Event::listen('UserWasCreated', function($event)
{
// 3. Update our newsletter
});
The great thing is separation of concerns. The command "createuser" now does not need to worry itself about what happens after a user is created. It just needs to CreateUser.
Also - if we want to add another function after a user signs up - say enter them in a lotto draw - you can just add another Event Class and add a new event listener.
EventClassC
Event::listen('UserWasCreated', function($event)
{
// 4. Register them in lotto
});
Notice how we didnt need to touch the command CreateUser class code at all? This provides a true separation concerns of classes in a OOP style approach.
I just want to share my understanding of this concept on top of the correct answer:
The main difference is that Commands can change a Model state, while Events just react to a state change.
COMMANDS:
Commands in Laravel represent the implementation of the Command design pattern.
The main adventages of Commands:
The can be accessed from anywhere
They are very easy to read by any other developer
To create a Command in Laravel 5:
You need to generate a command DTO (which can implement the SelfHandling interface). Using php artisan make:command {command-name}
Example: php artisan make:command Course/PostCourseCommand
The naming convention for commands: speak the business language and add postfix Command to it
To call (dispatch) the command from you controller, you can use:
$this->dispatch(new PostCourseCommand())
or
Bus::dispatch(new PostCourseCommand());
Side Note:
The "dispatch from request” feature is a nice way to skip passing the variables to the command constructor one by one, instead it will resolve this for you:
Example:
$test_request = Request::create('/test', 'GET', [
'name' => 'Mahmoud',
'nickname' => 'Mega'
]);
$result = Bus::dispatchFrom(
CreateCourse::class, $test_request
);
Finally:
You can separate the handler function and it’s logic from the command DTO to the Handlers directory, to do so:
Generate a command handler via artisan
art handler:command --command="Course/PoatCourseCommand"
remove the SelfHandling interface from the Command class so it will search for a handler to handle it.
EVENTS:
Events in Laravel represent the implementation of the Observer design pattern.
To create an Event in Laravel 5:
use artisan: art make:event {event-name}
Example: art make:event LogSomething
generate an event handler for that event
art handler:event LogSomething --event="LogSomething"
register the event and it’s handler in the event service provider (app/Providers/EventServiceProvider.php)
Example:
protected $listen = [
\Zzz\Events\LogSomething::class => [ // event.name
\Zzz\Handlers\Events\LogSomething::class, //EventListener
],
],
To call (fire) an Event:
use:
Event::fire(New LogSomething());
or you can use the event helper
event(New LogSomething());
Side Note:
alternatively you can generate an event by simply registering the event in the service provider then running this command.
php artisan event:generate << this will automatically add the two classes for you
Also you can listen to an event without creating an event handler or registering a lister in the listeners array, by just going to the event service prover and inside the boot function writing your event and its action (NOT RECOMMENDED). Example:
Event::listen('XXX\Events\DoSomethingElse', function($event)
{
dd('handle me :)');
});
Finally: you can queue an event or even subscribe to multiple events from within the class itself..
I'm currently using ClankBundle in my Symfony2 app and need to implement the Push to an Existing Site feature mentioned in the Ratchet documentation. However, I found no clues on how to accomplish this functionality using ClankBundle !
I have successfully implemented the Topic Handler Setup from the ClankBundle documentation, but need a way to add support for ZeroMQ.
Could you please demonstrate (preferably with code) how would one use ZeroMQ with Clankbundle?
I've found a workaround that might be useful to you too.
In my setup I have Periodic service that needs to broadcast new information to topic and I have custom topic handler.
First off inject custom topic handler in periodic service and save all subscribed topics in topic handler, something like this:
public function onSubscribe(Conn $conn, $topic)
{
if (!array_key_exists($topic->getId(), self::$subscribedTopics)) {
self::$subscribedTopics[$topic->getId()] = $topic;
}
$topic->broadcast('connected');
}
public function getSubscribedTopics() {
return self::$subscribedTopics;
}
In periodic fetch those topics and broadcast on them:
public function tick()
{
$subscribedTopics = $this->topic->getSubscribedTopics();
foreach($subscribedTopics as $subscribedTopic) {
$subscribedTopic->broadcast('yey');
}
}
I'm using the AWS SDK for PHP, and have a command-line tool waiting for a DB instance to be created with waitUntilDBInstanceAvailable():
$this->rdsClient->waitUntilDBInstanceAvailable([
'DBInstanceIdentifier' => 'test'
]);
Is there a way to register a callback function, so that every time the SDK polls RDS, my callback function is called?
Something like:
$this->rdsClient->waitUntilDBInstanceAvailable([
'DBInstanceIdentifier' => 'test',
'CallbackFunction' => function() {
echo '.';
}
]);
That would give the user some feedback about the fact that the script is still waiting, and did not hang arbitrarily.
The doc says:
The input array uses the parameters of the DescribeDBInstances operation and waiter specific settings
But I couldn't find out what these waiter specific settings are.
There is a page specifically about waiters in the AWS SDK for PHP User Guide. On that page it talks about how use event listeners with waiters. You need to interact directly with the waiter object.
// Get and configure the waiter object
$waiter = $client->getWaiter('BucketExists')
->setConfig(array('Bucket' => 'my-bucket'))
->setInterval(10)
->setMaxAttempts(3);
// Get the event dispatcher and register listeners for both events emitted by the waiter
$dispatcher = $waiter->getEventDispatcher();
$dispatcher->addListener('waiter.before_attempt', function () {
echo "Checking if the wait condition has been met…\n";
});
$dispatcher->addListener('waiter.before_wait', function () use ($waiter) {
$interval = $waiter->getInterval();
echo "Sleeping for {$interval} seconds…\n";
});
$waiter->wait();
// Also Licensed under version 2.0 of the Apache License.
You can do what you're looking for by implementing a custom waiter. Unfortunately, it's not as simple as supporting a callback on an existing waiter, but you can still implement what you're looking for.