Pusher (Real-time WebSockets)
The artiframe add pusher extension installs the official pusher/pusher-php-server package, allowing you to instantly broadcast backend events to your frontend (React, Vue, Vanilla JS, Mobile) over WebSocket using the src/Service/PusherService.php class.
It is indispensable for real-time notification systems, live chat applications, and live graphs.
Install the extension into your project via the terminal:
$ artiframe add pusher
After installation, the required configurations will be appended to your .env file. Fill these out with the information from your Pusher Dashboard:
PUSHER_APP_ID=your_app_id
PUSHER_APP_KEY=your_app_key
PUSHER_APP_SECRET=your_app_secret
PUSHER_APP_CLUSTER=eu
You define a Channel and an Event name. The frontend listens to that channel, and the backend fires the event.
<?php
use Src\Service\PusherService;
$pusher = new PusherService();
// Let's assume a new order just came in.
$orderData = [
'order_id' => 105,
'total' => '250.00 TL',
'customer' => 'Ali Veli'
];
// 1st Parameter: Channel Name, 2nd Parameter: Event Name, 3rd Parameter: Sent Data (Array)
$pusher->trigger('admin-channel', 'new-order', $orderData);
echo "Notification sent to Admin panel!";
This is an example showing how you receive the data on the client side (e.g., inside index.html) using pusher-js:
import Pusher from 'pusher-js';
// Connect using your APP KEY
const pusher = new Pusher('your_app_key', { cluster: 'eu' });
// Subscribe to the channel
const channel = pusher.subscribe('admin-channel');
// Listen for the event
channel.bind('new-order', function(data) {
alert("New order arrived! ID: " + data.order_id);
console.log(data);
});
If you don't want everyone to listen to your data (e.g., users viewing their own private messages), you use Private Channels. The channel name must begin with private-.
<?php
// Sending a private message to User ID 15
$pusher->trigger('private-user-15', 'new-message', [
'sender' => 'Ahmet',
'message' => 'Hello, how are you?'
]);
When the Frontend attempts to subscribe to private-user-15, Pusher sends an authorization request to your backend via an HTTP POST. You authenticate the user and return Pusher's signature.
<?php
use Src\Service\PusherService;
$pusher = new PusherService();
// Socket ID and Channel Name sent by Pusher
$socketId = $_POST['socket_id'];
$channelName = $_POST['channel_name'];
// Verify user's session (e.g., from session or JWT)
$activeUserId = $_SESSION['user_id'] ?? null;
// The user is only authorized to listen to their own channel
if ("private-user-{$activeUserId}" === $channelName) {
echo $pusher->authenticate($socketId, $channelName);
} else {
header('', true, 403);
echo "Forbidden";
}