-
Notifications
You must be signed in to change notification settings - Fork 30
Zeromq integration #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peec
wants to merge
3
commits into
JDare:master
Choose a base branch
from
peec:zeromq-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # Integration with ZMQ | ||
|
|
||
| Sometimes you would want to publish events from a controller to the Websocket server, for example a notification system. | ||
| See more of how it's integrated [here](http://socketo.me/docs/push). | ||
|
|
||
|
|
||
| ## Server setup | ||
|
|
||
| First, you should Install ZMQ, and the PHP bindings. This should be installed on your server. | ||
|
|
||
| Download ZMQ: | ||
|
|
||
| - http://zeromq.org/area:download | ||
|
|
||
| And the php bindings: | ||
|
|
||
| - http://zeromq.org/bindings:php | ||
|
|
||
|
|
||
|
|
||
|
|
||
| ## Configuration | ||
|
|
||
|
|
||
| Add `"react/zmq": "@stable"` to the `require` section of your composer.json file, and run | ||
| `composer update react/zmq`. | ||
|
|
||
|
|
||
|
|
||
| Add ZMQ as enabled: | ||
|
|
||
| ```yaml | ||
| clank: | ||
| topic: | ||
| # Example topic service: | ||
| - | ||
| name: "notification" | ||
| service: "acme.topic.notification" | ||
| # Enable ZMQ | ||
| zmq: | ||
| enabled: true | ||
| port: 5555 # This should not be the same port as the Clank(Ratchet) server. Default is 5555 | ||
| ``` | ||
|
|
||
| When starting the server with `clank:server` you should now see somthing like this: | ||
|
|
||
| ```bash | ||
| peec@dev:$ php app/console clank:server | ||
| Starting Clank | ||
| Launching Ratchet WS Server on: 0.0.0.0:8088 | ||
|
|
||
| Listening to ZMQ messages on tcp://127.0.0.1:5555 | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
|
|
||
| ## Sending messages from the controller | ||
|
|
||
| From any controller, we can now send messages like this: | ||
|
|
||
| ```php | ||
| $this->container->get('jdare_clank.zmq.dispatcher')->send(new \JDare\ClankBundle\Zmq\ZmqMessage( | ||
| "notification", // Reference the topic service associated with this message. | ||
| ["hello" => "World"] // Some data | ||
| )); | ||
| ``` | ||
|
|
||
| ## Subscribing to ZMQ Messages. | ||
|
|
||
| ```php | ||
| use JDare\ClankBundle\Topic\TopicInterface; | ||
| use JDare\ClankBundle\Zmq\ZMQMessageReciever; | ||
| use Ratchet\ConnectionInterface as Conn; | ||
| use Ratchet\Wamp\Topic; | ||
|
|
||
|
|
||
| class NotificationTopic implements TopicInterface, ZMQMessageReciever | ||
| { | ||
|
|
||
|
|
||
| // .. other onX functions here.. | ||
|
|
||
| /** | ||
| * When a ZMQ message is recieved for this Topic, this handler is called. | ||
| * @param Topic $topic The Topic instance | ||
| * @param $data | ||
| * @return mixed | ||
| */ | ||
| public function onZMQMessage(Topic $topic, $data) { | ||
| print_r($data); // Prints the data recieved in the console. Only if someone has subscribed to this channel. | ||
| $topic->broadcast($data); | ||
| } | ||
|
|
||
| } | ||
| ``` | ||
|
|
||
| ## Example simple client | ||
|
|
||
| - You must include the JS files, they are in the bundles Resources folder. | ||
| - Change `ws://127.0.0.1:8088` to your clank server definition. | ||
|
|
||
| ```html | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <script src="Clank.js"></script> | ||
| <script src="autobahn.min.js"></script> | ||
| </head> | ||
| <body> | ||
| <script> | ||
| var myClank = Clank.connect("ws://127.0.0.1:8088"); | ||
|
|
||
| myClank.on("socket/connect", function(session){ | ||
| //session is an Autobahn JS WAMP session. | ||
|
|
||
| console.log("Successfully Connected!"); | ||
|
|
||
| //the callback function in "subscribe" is called everytime an event is published in that channel. | ||
| session.subscribe("notification/channel", function(uri, payload){ | ||
| console.log("Received message", payload); | ||
| }); | ||
| }) | ||
|
|
||
| myClank.on("socket/disconnect", function(error){ | ||
| //error provides us with some insight into the disconnection: error.reason and error.code | ||
|
|
||
| console.log("Disconnected for " + error.reason + " with code " + error.code); | ||
| }) | ||
|
|
||
|
|
||
| </script> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
|
|
||
| Now open a controller where you use `jdare_clank.zmq.dispatcher` and you should see the message in the client (console.log). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| /** | ||
| * Created by PhpStorm. | ||
| * User: peecdesktop | ||
| * Date: 18.09.14 | ||
| * Time: 19:56 | ||
| */ | ||
|
|
||
| namespace JDare\ClankBundle\Zmq; | ||
|
|
||
|
|
||
| use Ratchet\Wamp\Topic; | ||
|
|
||
| interface ZMQMessageReciever { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is a typo here : Shoud'nt it be ZMQMessageReceiver ? |
||
|
|
||
| /** | ||
| * When a ZMQ message is recieved for this Topic, this handler is called. | ||
| * @param Topic $topic The Topic instance | ||
| * @param $data | ||
| * @return mixed | ||
| */ | ||
| public function onZMQMessage (Topic $topic, $data); | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add the possibility in the config to listen on something else than
127.0.0.1? For exampletcp://{$zmq['listen_to_host']}:{$zmq['port']}withlisten_to_hostset to*Thanks !