Download the PHP package sebk/small-events-swoft without Composer
On this page you can find all versions of the php package sebk/small-events-swoft. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sebk/small-events-swoft
More information about sebk/small-events-swoft
Files in sebk/small-events-swoft
Package small-events-swoft
Short Description Swoft cross applications events dispatcher and receiver
License GPL-3.0-only
Informations about the package small-events-swoft
sebk/small-events-swoft
Swoft cross applications events dispatcher and receiver
Install
Create your Swoft project : http://swoft.io/docs/2.x/en/quick-start/install.html
Require package (https://github.com/sebk69/small-events-swoft) :
Documentation
Supported connectors
For now, only RabbitMq is supported.
In future, Apache Kafka and MySql connectors are planned.
Configuration
Add contract to choose your massage brocker to your 'bean.php' :
Here RabbitMq message broker will be used.
Create a 'small_events.php' file in your config dir :
The applicationId must be unique between all your applications : it will be used to create a queue to receive events.
In the RabbitMq section, you can set server host, user and password.
The smallEventsExchange is the exchane that will be used to dispatch events in all your applications.
In the queues array, declare all queues that you will use for events or messaging. The first queue is dedicated to your events flow and must be declared :
- The first parameter must be ['Sebk\SmallEventsSwoft\Event\Config::EVENT_EXCHANGE'][dot][your applicationId]
- The second one is the definition of events exchange, must be defined as the 'smallEventsExchange' parameter
The second queue can be used to send messages outside event flow.
The consumers array must contain the list of consumers objects, used to listen all queues outside events queue (see below for how to create consumer class).
Send a message outside event flow
Their is a simple method to send messages to other applications :
This command will send $message to the exchange of 'test' queue.
Send an event
The class SmallEvent, allow you to create an event that will be shared with all listening applications :
The first parameter is the event name and the second parameter is the data of your event.
Note that the data must be json serializable.
Then use the dispatcher to send event to other applications (including yours) :
Create consumers
The simple messages (not event one) can be consumed by creating consumers (the ones we have seen in configuation section)
Here is an example :
A consumer must extends 'AbstractSmallConsumer' class and define the queue name to listen in constructor.
The method 'consume' will be called at every time a message is received from the queue. It must return true to ack message.
Listen to a queue
You must use the following command to listen a queue :
This command will wait for messages of teh queue 'test' and call your consumer class at each message.
If you don't use --queue parameter, the proccess will listen to events.
To manage your workers, you can use tools like Supervisor : http://supervisord.org
And events ? How to subscribe
Events are dispatched via Swoft/Event standard package (http://swoft.io/docs/2.x/en/event/usage.html).
Here is a small example to subscibe to our 'testEvent' :
All versions of small-events-swoft with dependencies
php-amqplib/php-amqplib Version ~3.1.0
swoft/event Version ~2.0.0