Working with an API gateway via WebSocket
To establish a WebSocket connection to an API gateway:
If you no longer need the resources you created, delete them.
Getting started
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Required paid resources
The cost of the resources includes the fee for the number of API gateway requests and outbound traffic (see Yandex API Gateway pricing).
Create an API gateway
-
In the management console
, select the folder where you want to create an API gateway. -
In the list of services, select API Gateway.
-
Click Create API gateway.
-
In the Name field, enter
websocket
. -
Optionally, in the Description field, provide a description.
-
In the Specification section, add the specification:
openapi: 3.0.0 info: title: Test API version: 1.0.0 paths: /connections: x-yc-apigateway-websocket-message: summary: Get connection identifier operationId: getConnectionID parameters: - name: X-Yc-Apigateway-Websocket-Connection-Id in: header description: Websocket connection identifier required: true schema: type: string responses: '200': description: Connection identifier content: text/plain: schema: type: string x-yc-apigateway-integration: type: dummy http_code: 200 http_headers: Content-Type: application/json content: text/plain: '{"connection_id":"{X-Yc-Apigateway-Websocket-Connection-Id}"}'
-
Click Create.
Establish a connection
-
Open the terminal and install wscat
:npm install -g wscat
-
Connect to the API gateway. Replace
<API_gateway_domain>
, with the API gateway domain formatted asd5dm1lba80md********.i9******.apigw.yandexcloud.net
:wscat -c wss://<API_gateway_domain>/connections Connected (press CTRL+C to quit)
-
Type a message and press
Enter
.
The message will go to the API gateway over the established connection. The API gateway will call integration and return a response that you will see on your screen. The response will contain the established connection ID:
> Hello!
< {"connection_id":"<connection_ID>"}
Test the connection
Open a new terminal window and test the connection using the connection ID you got.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID>
command. You can also set a different folder for any specific command using the --folder-name
or --folder-id
parameter.
-
Retrieve information on the established connection:
yc serverless api-gateway websocket get <connection_ID>
-
Send a message to the client:
yc serverless api-gateway websocket send <connection_ID> --data Hello!
-
Terminate the connection:
yc serverless api-gateway websocket disconnect <connection_ID>
-
Go to the terminal window with the established connection. It should display the following information:
wscat -c wss://<API_gateway_domain>/connections Connected (press CTRL+C to quit) > Hello! < {"connection_id":"<connection_ID>"} < Hello! Disconnected (code: 1000, reason: "")
Where
API_gateway_domain
is a string formatted asd5dm1lba80md********.i9******.apigw.yandexcloud.net
.
How to delete the resources you created
To stop paying for the resources created, delete the API gateway.