An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
Use Azure Web PubSub as the real-time messaging layer between the Laravel backend (Web App) and the Static Web App frontend. The high-level flow is:
- The frontend (Static Web App) opens a WebSocket connection to Azure Web PubSub.
- The backend (Laravel in Web App) uses Web PubSub’s REST APIs/SDK to send notifications to hubs/groups/users.
- Web PubSub fan-outs messages to connected clients.
A practical setup based on the documented patterns:
- Design hubs, groups, and users
- Create at least one hub for notifications, for example
notifications. - Use groups for logical channels (for example, per-tenant or per-feature groups) and/or
userIdto target specific users. - Concepts are defined in the service as:
- Hub = logical set of client connections (for example, chat or notification hub).
- Group = subset of connections in a hub (for example, a room or topic).
- User = identity that can own multiple connections (for example, same user on multiple devices).
- Create at least one hub for notifications, for example
- Connect the Static Web App frontend
- The frontend connects using the standard WebSocket protocol directly to Web PubSub.
- For client-side pub/sub without always going through the backend, use the
json.webpubsub.azure.v1subprotocol:const ws = new WebSocket( "wss://<your-service>.webpubsub.azure.com/client/hubs/notifications", "json.webpubsub.azure.v1" ); - After connection, the client can:
- Join groups by sending JSON messages like:
{ "type": "joinGroup", "group": "<group_name>" } - Receive messages that Web PubSub broadcasts to that group or hub.
- Join groups by sending JSON messages like:
- Connect the Laravel backend (Web App)
- The backend does not need to maintain WebSocket connections itself; it uses Web PubSub’s server-side APIs to manage connections and send messages.
- From PHP, call the Web PubSub REST APIs to:
- Broadcast to all connections in a hub.
- Send to a specific group.
- Send to a specific user.
- Typical pattern:
- When an event occurs in Laravel (for example, new notification), call Web PubSub REST endpoint to send a message to the relevant hub/group/user.
- Serverless/event-driven option (if adding Azure Functions)
- Instead of (or in addition to) calling REST directly from Laravel, an Azure Function can be used as an event handler:
- Web PubSub triggers events like
connected,disconnected, andmessageto the function. - The function can then push notifications, persist messages, or route them to other services.
- Web PubSub triggers events like
- This pattern is documented for building serverless chat/notification apps and can be adapted to Laravel as the upstream system.
- Instead of (or in addition to) calling REST directly from Laravel, an Azure Function can be used as an event handler:
- Security and connection management
- Use hubs and groups to control which notifications each client receives.
- Protect Web PubSub connection strings and keys; treat them like root passwords.
- In production, store keys in a secure store such as Azure Key Vault and avoid hard-coding them in Laravel or frontend code.
- Typical end-to-end flow for notifications
- User opens the Static Web App → frontend JS connects to Web PubSub hub and joins one or more groups.
- User performs an action or a background process in Laravel triggers a notification.
- Laravel calls Web PubSub REST API to send a message to the target hub/group/user.
- Web PubSub delivers the message in real time to all connected clients in that hub/group.
- Frontend receives the message over WebSocket and updates the UI (for example, shows a toast or updates a notifications list).
This architecture keeps WebSockets and connection fan-out in Web PubSub, while Laravel focuses on business logic and the Static Web App focuses on UI.
References:
- Azure Web PubSub basic concepts
- Azure Web PubSub service internals
- What is Azure Web PubSub service?
- Event notifications from clients
- Tutorial: Create a serverless chat app with Azure Web PubSub service and Azure Static Web Apps
- Tutorial: Create a serverless notification app with Azure Functions and Azure Web PubSub service