|
| 1 | +--- |
| 2 | +title: Integrate - How to use Web PubSub for Socket.IO with Azure API Management |
| 3 | +description: A how-to guide about how to use Web PubSub for Socket.IO with Azure API Management |
| 4 | +keywords: Socket.IO, Socket.IO on Azure, webapp Socket.IO, Socket.IO integration, APIM |
| 5 | +author: zackliu |
| 6 | +ms.author: chenyl |
| 7 | +ms.service: azure-web-pubsub |
| 8 | +ms.custom: devx-track-azurecli |
| 9 | +ms.topic: tutorial |
| 10 | +ms.date: 1/11/2024 |
| 11 | +--- |
| 12 | +# How-to: Use Web PubSub for Socket.IO with Azure API Management |
| 13 | + |
| 14 | +Azure API Management service provides a hybrid, multicloud management platform for APIs across all environments. This article shows you how to add real-time capability to your application with Azure API Management and Web PubSub for Socket.IO. |
| 15 | + |
| 16 | +:::image type="content" source="./media/socket-io-howto-integrate-apim/sio-apim.png" alt-text="Diagram that shows the architecture of using Web PubSub for Socket.IO with API Management."::: |
| 17 | + |
| 18 | +## Limitations |
| 19 | + |
| 20 | +Socket.IO clients support WebSocket and Long Polling and by default, the client will connect to the service with Long Polling and then upgrade to WebSocket. However, as for now, API Management doesn't yet support different types of APIs (WebSocket or Http) with the same path. You must only either `websocket` or `polling` in client settings. |
| 21 | + |
| 22 | +## Create resources |
| 23 | + |
| 24 | +In order to follow the step-by-step guide, you need |
| 25 | + |
| 26 | +- Follow [Create a Web PubSub for Socket.IO resource](./socketio-quickstart.md#create-a-web-pubsub-for-socketio-resource) to create a Web PubSub for Socket.IO instance. |
| 27 | +- Follow [Quickstart: Use an ARM template to deploy Azure API Management](../api-management/quickstart-arm-template.md) and create an API Management instance. |
| 28 | + |
| 29 | +## Set up API Management |
| 30 | + |
| 31 | +### Configure APIs when client connects with `websocket` transport |
| 32 | + |
| 33 | +This section describes the steps to configure API Management when the Socket.IO clients connect with `websocket` transport. |
| 34 | + |
| 35 | +1. Go to **APIs** tab in the portal for API Management instance, select **Add API** and choose **WebSocket**, **Create** with the following parameters: |
| 36 | + |
| 37 | + - Display name: `Web PubSub for Socket.IO` |
| 38 | + - Web service URL: `wss://<your-webpubsubforsocketio-service-url>/clients/socketio/hubs/eio_hub` |
| 39 | + - API URL suffix: `clients/socketio/hubs/eio_hub` |
| 40 | + |
| 41 | + The hub name can be changed to meet your application. |
| 42 | + |
| 43 | +1. Press **Create** to create the API and after created, switch to **Settings** tab and uncheck **Subscription required** for quick demo purpose |
| 44 | + |
| 45 | +### Configure APIs when client connects with `polling` transport |
| 46 | + |
| 47 | +This section describes the steps to configure API Management when the Socket.IO clients connect with `websocket` transport. |
| 48 | + |
| 49 | +1. Go to **APIs** tab in the portal for API Management instance, select **Add API** and choose **WebSocket**, **Create** with the following parameters: |
| 50 | + |
| 51 | + - Display name: `Web PubSub for Socket.IO` |
| 52 | + - Web service URL: `https://<your-webpubsubforsocketio-service-url>/clients/socketio/hubs/eio_hub` |
| 53 | + - API URL suffix: `clients/socketio/hubs/eio_hub` |
| 54 | + |
| 55 | + The hub name can be changed to meet your application. |
| 56 | + |
| 57 | +1. Switch to **Settings** tab and uncheck Subscription required for quick demo purpose |
| 58 | + |
| 59 | +1. Switch to **Design** tab and select **Add operation**, and Save with the following parameters: |
| 60 | + |
| 61 | + Add operation for post data |
| 62 | + - Display name: connect |
| 63 | + - URL: POST / |
| 64 | + |
| 65 | + Add operation for get data |
| 66 | + - Display name: connect get |
| 67 | + - GET / |
| 68 | + |
| 69 | +## Try Sample |
| 70 | + |
| 71 | +Now, the traffic can reach Web PubSub for Socket.IO through API Management. There are some configurations in application. Let’s use a chat application as an example. |
| 72 | + |
| 73 | +Clone Github repo https://github.com/Azure/azure-webpubsub and investigate to `sdk/webpubsub-socketio-extension/examples/chat` folder |
| 74 | + |
| 75 | +Then make some changes to let the sample work with API Management |
| 76 | + |
| 77 | +1. Open `public/main.js` and it's the Socket.IO client side codes |
| 78 | + |
| 79 | + Edit the constructor of Socket.IO. Note that you have to select either `websocket` or `polling` as the transport: |
| 80 | + |
| 81 | + ```javascript |
| 82 | + const webPubSubEndpoint = "https://<api-management-url>"; |
| 83 | + var socket = io(webPubSubEndpoint, { |
| 84 | + transports: ["websocket"], // Depends on your transport choice. If you use WebSocket in API Management, set it to "websocket". If choosing Long Polling, set it to "polling" |
| 85 | + path: "/clients/socketio/hubs/eio_hub", // The path also need to match the settings in API Management |
| 86 | + }); |
| 87 | + ``` |
| 88 | + |
| 89 | +2. On the **Keys** tab of Web PubSub for Socket.IO. Copy the **Connection String** and use the following command to run the server: |
| 90 | + |
| 91 | + ```bash |
| 92 | + npm install |
| 93 | + npm run start -- <connection-string> |
| 94 | + ``` |
| 95 | + |
| 96 | +3. According to the output, use browser to visit the endpoint |
| 97 | + |
| 98 | + ``` |
| 99 | + Visit http://localhost:3000 |
| 100 | + ``` |
| 101 | + |
| 102 | +4. In the sample, you can chat with other users. |
| 103 | + |
| 104 | +## Next steps |
| 105 | +> [!div class="nextstepaction"] |
| 106 | +> [Check out more Socket.IO samples](https://aka.ms/awps/sio/sample) |
0 commit comments