AckNotificationBundle
Introduction
This bundle offers a predefined architecture for a notification system using Redis and Node.js, (*1)
Everything is based on the Pub/Sub (publish/subscribe) system of Redis, here is simple diagram of what happen behind the scene:, (*2)
, (*3)
Each messages contains a content rendered by twig and an array of the users id., (*4)
Installation
Use composer :, (*5)
php composer.phar require ack/notification-bundle
Register the bundle in your app/AppKernel.php file :, (*6)
$bundles = array(
...
new Ack\NotificationBundle\AckNotificationBundle(),
...
);
If you already have a server node running on your application you have an example of implementation in example_server.js, (*7)
Else, after the assets install, you can go in /web/bundles/acknotification/nodejs and:, (*8)
npm install
node server.js
Usage
From a controller or anywhere you have access to the 'ack.notifier' service:, (*9)
$this->get('ack.notifier')->notify(
':notification:test.html.twig', // Any twig file
array(1, 2, 3), // Array of the users id that need to be notified, use '*' if you want to notify everyone (anonymous users included)
array() // Optional parameters according the your twig view
);
Do not forget to load socket.io.js and connect to the server., (*10)
After you have emitted the 'loaded' event from your frontend, Node.js will catch it and store your user in a Redis hash.
That way we have a list of the online users somewhere and each hash contains the socketId., (*11)
<script>
socket.emit('loaded', {
id : '{{ app.user is not null ? app.user.id : "anon." }}'
});
</script>
Once Node.js receive a notification, it emits an event 'notification' to each users id, you can do that kind of script in your frontend, to notify users., (*12)
socket.on('notification', function (notification) {
// get <div> "notifications" and append notification
});
This bundle has a dependency on snc redis, so do not forget to add this on your config.yml
I recommend using redis for your other needs such as session storing, caching, logging and more here: SncRedisBundle, (*13)
snc_redis:
clients:
default:
type: predis
alias: default
dsn: redis://localhost
logging: %kernel.debug%