Working with API requests
VoxEngine facilitates communication between our cloud and third-party services or APIs through the Net namespace. Additionally, it provides guidance on creating and handling HTTP requests, as well as sending emails, within your JavaScript cloud scenarios.
HTTP API requests
Two methods that allow performing requests: httpRequest and httpRequestAsync.
The httpRequest method performs synchronous HTTP requests. Its second parameter is a callback function that can accept the HttpRequestResult object and process the object’s properties.
The httpRequestAsync method performs asynchronous HTTP requests, i.e., the method returns a Promise that resolves with the HttpRequestResult object.
Timeouts and default verb
By default, both methods use the GET verb to perform requests. TCP connect timeout is 6 seconds, and total request timeout is 90 seconds.
WebSocket connections
In addition to HTTP API requests, VoxEngine allows developers to use the WebSocket protocol for two-way communication.
To send data via the WebSocket protocol:
- Require the
Modules.WebSocketmodule in your scenario - Create a WebSocket connection via the VoxEngine.createWebSocket method
- Use the webSocket.send method to send text data into the connection
- Subscribe to WebSocketEvents, such as
MESSAGE, to process the replies.
Here is an example of sending text data to a WebSocket connection:
You can find more information about uning the WebSocket protocol with VoxEngine scenarios in the Sending media over WebSockets article.
Sending emails
Sometimes it is convenient or even necessary to send emails to an external service. To do that, you can use either the sendMail or sendMailAsync methods. Both of them process the SendMailResult object after a successful call.
For asynchronous sending, use this: