An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
Hi @Tengku Aiman,
Thanks for reaching out! Azure Web PubSub is an excellent fit for handling WebSocket-based notifications in your setup. Your Laravel app can securely handle authentication and token generation, while your Static Web Apps frontend connects directly to PubSub for real-time updates.
Recommended Architecture:
- Laravel Backend – Authenticates the user and exposes a protected /negotiate endpoint that returns a short-lived Client Access URL (with JWT token).
- Azure Web PubSub – Manages the WebSocket connections and message delivery (to specific users, groups, or broadcast).
- Static Web Apps Frontend – Calls your negotiate endpoint, then opens a WebSocket connection to PubSub to receive notifications.
This keeps your connection string secure on the server side.
Quick Implementation Steps:
In Laravel (Negotiate Endpoint): Create a protected route that calls the Web PubSub REST API to generate a token for the authenticated user.
public function negotiate()
{
$userId = auth()->id();
// Parse connection string and call POST {endpoint}/api/hubs/{hub}/:generateToken
// Return the full client URL: wss://<your-service>.webpubsub.azure.com/client/hubs/{hub}?access_token=...
}
Publishing Notifications: Use the REST API from Laravel to send messages to specific users or groups.
In your Static Web Apps (JavaScript):
const response = await fetch('/negotiate');
const { url } = await response.json();
const ws = new WebSocket(url);
ws.onmessage = (event) => {
// Handle incoming notification and update UI
};
This pattern follows standard best practices for secure real-time apps.
Reference:
https://learn.microsoft.com/en-us/azure/azure-web-pubsub/howto-generate-client-access-url
https://learn.microsoft.com/en-us/rest/api/webpubsub/dataplane/web-pub-sub/generate-client-token
https://learn.microsoft.com/en-us/azure/azure-web-pubsub/howto-websocket-connect
https://learn.microsoft.com/en-us/azure/azure-web-pubsub/tutorial-serverless-static-web-app
Kindly let us know if the above helps or you need further assistance on this issue.
Please "accept" if the information helped you. This will help us and others in the community as well.