Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AggelosGalatis's avatar

Web push notifications

Hello. I created a ticket system in Laravel. There are three roles there. A simple user who can only creates a ticket, an agent and a supervisor which both of them can assign a tickets, edit a ticket etc. I want when a simple user creates a ticket, all the agents and supervisor that they are connected to get the notification. As a first step when i create a ticket i get also the notification. I created with the Notification API of the web browser. I want to notify all the users, not only me. Do i have to use Push API also or can i create a websocket and get the notification and after to display it with Notification API?

0 likes
1 reply
LaryAI's avatar
Level 58

To notify all connected users when a new ticket is created, you can use a combination of websockets and the Notification API. Here's how you can implement it:

  1. Set up a websocket server: You can use a library like Ratchet or Laravel WebSockets to set up a websocket server. This server will listen for incoming connections and broadcast messages to all connected clients.

  2. When a new ticket is created, send a message to the websocket server: In your Laravel application, when a new ticket is created, you can send a message to the websocket server with the details of the ticket. You can use a library like Guzzle to make a POST request to the websocket server with the ticket data.

  3. Broadcast the message to all connected clients: When the websocket server receives the message, it should broadcast it to all connected clients. You can use the broadcast method provided by the websocket library to broadcast the message.

  4. Display the notification using the Notification API: When a client receives the message, it can use the Notification API to display a notification to the user. You can use the Notification.requestPermission() method to request permission from the user to display notifications. Once permission is granted, you can use the new Notification() constructor to create and display the notification.

Here's an example implementation using Laravel WebSockets:

  1. Install Laravel WebSockets:
composer require beyondcode/laravel-websockets
php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="migrations"
php artisan migrate
  1. Set up a websocket route:
// routes/websockets.php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Broadcast;

Broadcast::routes(['middleware' => ['auth:api']]);

Broadcast::channel('tickets', function ($user) {
    return ['id' => $user->id];
});
  1. Send a message to the websocket server when a new ticket is created:
// app/Http/Controllers/TicketController.php

use GuzzleHttp\Client;

public function store(Request $request)
{
    // create the ticket
    $ticket = Ticket::create($request->all());

    // send a message to the websocket server
    $client = new Client();
    $client->post('http://localhost:6001/api/tickets', [
        'json' => $ticket,
        'headers' => [
            'Authorization' => 'Bearer ' . auth()->user()->api_token,
        ],
    ]);

    // return the response
    return response()->json($ticket);
}
  1. Broadcast the message to all connected clients:
// app/WebSockets/TicketMessageHandler.php

use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Messages\PusherMessage;

class TicketMessageHandler
{
    public function __invoke(PusherMessage $message, ChannelManager $channelManager)
    {
        $ticket = $message->data;

        $channelManager->channel('tickets')->broadcast([
            'title' => 'New ticket created',
            'body' => $ticket['title'],
        ]);
    }
}
  1. Display the notification using the Notification API:
// resources/js/app.js

if ('Notification' in window) {
    Notification.requestPermission();
}

Echo.channel('tickets')
    .listen('.broadcast', function (data) {
        new Notification(data.title, {
            body: data.body,
        });
    });

This implementation assumes that you have set up authentication and API tokens for your Laravel application. You may need to modify the code to fit your specific use case.

Please or to participate in this conversation.