An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
Thank you for reaching us regarding the issue that you’d like to use Azure Web PubSub to power real-time notifications in your Laravel (PHP 8.2) app but you’re not sure how to wire up the client/server connections.
Since there is no official PHP SDK, we use a simple and clean pattern: your Laravel app generates connection tokens and publishes messages, while the browser connects directly to Web PubSub.
How It Works
- Your Laravel backend authenticates the user and provides a short-lived Client Access URL (with JWT token).
- The frontend uses that URL to open a WebSocket connection to Azure Web PubSub.
- When something happens in your app, Laravel publishes the notification via REST API, and Web PubSub delivers it instantly to the right clients.
Recommended Implementation Steps:
- Create the Web PubSub Resource In the Azure Portal, create a Web PubSub resource and choose a hub name (e.g., notifications). Copy the connection string, this will only be used on your server side.
- Backend Setup (Laravel) Store the connection string securely in your environment configuration. Implement a negotiate endpoint that returns a Client Access URL for the authenticated user. Add logic to publish messages (to individual users, groups, or all connections) when notifications need to be sent.
- Frontend Setup From the browser, call your Laravel negotiate endpoint to retrieve the access URL. Use it to open a WebSocket connection and listen for incoming messages.
- Security Best Practices
- Never expose the connection string to the client side.
- Always generate short-lived tokens from your backend.
- Protect the negotiate endpoint with authentication.
Microsoft Documentation for Reference:
- Key Concepts in Azure Web PubSub
- How to Generate Client Access URL
- Client Protocols
- REST API for Sending Messages
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.