ack/notification-bundle
Symfony bundle providing a Redis + Node.js (socket.io) pub/sub notification architecture. Render notifications via Twig, publish to specific user IDs or everyone, track online users in Redis, and push real-time “notification” events to connected clients.
Installation
composer require ack/notification-bundle
Register the bundle in AppKernel.php:
new Ack\NotificationBundle\AckNotificationBundle(),
Redis & Node.js Server
redis-server)./web/bundles/acknotification/nodejs and run:
npm install
node server.js
1337 (default).First Notification
In a controller, inject and use the ack.notifier service:
$this->get('ack.notifier')->notify(
':notification:test.html.twig', // Path to Twig template
[1, 2, 3], // User IDs to notify
['param' => 'value'] // Optional template variables
);
Frontend Integration Include Socket.IO in your layout:
<script src="http://your.domain:1337/socket.io/socket.io.js"></script>
<script src="/bundles/acknotification/js/notifications.js"></script>
Publish Notifications
Use the ack.notifier service to dispatch notifications from anywhere in the app (controllers, services, commands):
$notifier = $this->get('ack.notifier');
$notifier->notify(':notification:alert.html.twig', [userId], ['urgent' => true]);
Template Design
:notification: namespace (e.g., Resources/views/notification/alert.html.twig).{{ content }} in templates to render dynamic content passed via the 3rd argument of notify().User-Specific Logic
[1, 2, 3]).'*'.Frontend Handling
Extend notifications.js to customize how notifications appear:
$(document).ready(function() {
var socket = io.connect('http://your.domain:1337');
socket.on('notification', function(data) {
// Custom logic (e.g., show toast, update UI)
console.log('New notification:', data);
});
});
Conditional Notifications Use services to filter users dynamically:
$userIds = $userService->getActiveUsers();
$notifier->notify(':notification:update.html.twig', $userIds);
Background Processing
Dispatch notifications asynchronously via Symfony’s Messenger or queues (if extended).
Notification Types
Group templates by type (e.g., :notification:email/, :notification:push/), then route accordingly in the Node server.
Redis Connection Issues
redis_host in config.yml if needed:
ack_notification:
redis_host: 127.0.0.1
redis_port: 6379
redis-cli PING and check Node server logs for connection errors.Node Server Crashes
ps aux | grep node). Restart with:
pm2 start server.js --name ack-notification
server.js:
process.on('uncaughtException', function(err) {
console.error('Node error:', err);
});
Twig Template Paths
:notification: (e.g., :notification:test.html.twig). Relative paths (e.g., notification/test.html.twig) will fail.CORS Restrictions
io.set('origins', 'http://your.domain:*');
Log Notifications Extend the Node server to log published messages:
socket.on('notification', function(data) {
console.log('Sent to users:', data.users, 'Content:', data.content);
});
Check Redis Channels
Use redis-cli MONITOR to verify messages are published to the correct channel (default: ack_notification).
Frontend Debugging
Inspect network requests in DevTools (socket.io tab) to confirm messages are received.
Custom Node Server
Override server.js to add features (e.g., rate limiting, authentication):
// Example: Filter notifications by user role
socket.on('notification', function(data) {
if (data.users.includes('*') || userService.isAdmin(data.users[0])) {
io.to(data.users).emit('notification', data);
}
});
Multiple Notification Types Extend the bundle to support different channels (e.g., WebSocket, Firebase):
// In a custom service
$notifier->notify('firebase', ':notification:push.html.twig', [1, 2]);
Symfony Messenger Integration Create a custom transport to decouple notifications from the controller layer:
// config/packages/messenger.yaml
framework:
messenger:
transports:
notification: '%env(MESSENGER_TRANSPORT_DSN)%'
routing:
'App\Message\NotificationMessage': notification
How can I help you explore Laravel packages today?