Skip to content

Commit 266406c

Browse files
authored
docs: Document HTTP transport for the SQL API + describe transports in the REST API (#9616)
1 parent c9ebfbc commit 266406c

File tree

8 files changed

+280
-173
lines changed

8 files changed

+280
-173
lines changed

docs/pages/product/apis-integrations/recipes/_meta.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ module.exports = {
33
"cast-numerics": "Numeric values",
44
"sorting": "Custom sorting",
55
"pagination": "Pagination",
6-
"drilldowns": "Drilldowns"
6+
"drilldowns": "Drilldowns",
7+
"real-time-data-fetch": "Real-time data fetch"
78
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Real-Time data fetch in the REST API
2+
3+
## Use case
4+
5+
When building an embedded analytics application, you'd like to provide a real-time
6+
experience to the users. On some page, you'd like to display a chart that updates
7+
as soon as the data changes in the database.
8+
9+
## Configuration
10+
11+
When using the [REST API][ref-rest-api], you can use the [WebSocket
12+
transport][ref-websocket-transport] to receive real-time updates. Using this
13+
transport enables subscriptions to real-time updates.
14+
15+
### Client code
16+
17+
JavaScript example:
18+
19+
```javascript
20+
import cube from "@cubejs-client/core";
21+
import WebSocketTransport from "@cubejs-client/ws-transport";
22+
23+
const cubeApi = cube({
24+
transport: new WebSocketTransport({
25+
authorization: CUBE_TOKEN,
26+
apiUrl: "ws://localhost:4000/",
27+
}),
28+
});
29+
30+
// Create a subscription
31+
const subscription = cubeApi.subscribe(
32+
{
33+
measures: ["logs.count"],
34+
timeDimensions: [
35+
{
36+
dimension: "logs.time",
37+
granularity: "hour",
38+
dateRange: "last 1440 minutes",
39+
},
40+
],
41+
},
42+
options,
43+
(error, resultSet) => {
44+
if (!error) {
45+
// handle the update
46+
}
47+
}
48+
);
49+
50+
// Later on, unsubscribe from subscription
51+
subscription.unsubscribe();
52+
```
53+
54+
React example:
55+
56+
```javascript
57+
import { useCubeQuery } from "@cubejs-client/react";
58+
59+
const Chart = ({ query }) => {
60+
const { resultSet, error, isLoading } = useCubeQuery(query, {
61+
// The component will automatically unsubscribe when unmounted
62+
subscribe: true,
63+
});
64+
65+
if (isLoading) {
66+
return <div>Loading...</div>;
67+
}
68+
69+
if (error) {
70+
return <pre>{error.toString()}</pre>;
71+
}
72+
73+
if (!resultSet) {
74+
return null;
75+
}
76+
77+
return <LineChart resultSet={resultSet} />;
78+
};
79+
```
80+
81+
### Refresh rate
82+
83+
Real-time data fetch obeys the [`refresh_key`][ref-refresh-key].
84+
In order to provide a desired refresh rate, `refresh_key` should reflect the rate of
85+
change of the underlying data set; the querying time should also be much less than the
86+
desired refresh rate. Please use the [`every`][ref-every] parameter to adjust the
87+
refresh interval.
88+
89+
90+
[ref-rest-api]: /product/apis-integrations/rest-api
91+
[ref-websocket-transport]: /product/apis-integrations/rest-api#websocket-transport
92+
[ref-refresh-key]: /product/caching#refresh-keys
93+
[ref-every]: /product/data-modeling/reference/cube#refresh_key

docs/pages/product/apis-integrations/rest-api.mdx

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ REST API also provides endpoints for [GraphQL API][ref-graphql-api] and
3333
[Orchestration API][ref-orchestration-api]. However, they target specific use
3434
cases and are not considered part of the REST API.
3535

36-
## Example request
36+
## Transport
37+
38+
The REST API supports the following transports:
39+
40+
| Transport | Description | When to use |
41+
| --- | --- | --- |
42+
| HTTP | HTTP-based protocol with long polling | Use by default |
43+
| WebSocket | [WebSocket][link-websocket]-based protocol with support for real-time updates | Use for applications that require subscriptions to real-time updates |
44+
45+
### HTTP transport
3746

3847
You can use the `curl` utility to execute requests to the REST API:
3948

@@ -88,6 +97,36 @@ curl \
8897
http://localhost:4000/cubejs-api/v1/meta | jq
8998
```
9099

100+
You can also use the [JavaScript SDK][ref-javascript-sdk] to call the REST API
101+
from your JavaScript applications.
102+
103+
### WebSocket transport
104+
105+
WebSocket can be used to provide real-time experience. You can configire it via the
106+
`CUBEJS_WEB_SOCKETS` environment variable.
107+
108+
Clients using the [JavaScript SDK][ref-javascript-sdk] can be switched to WebSocket
109+
by passing `WebSocketTransport` to the `CubeApi` constructor:
110+
111+
```javascript
112+
import cube from "@cubejs-client/core"
113+
import WebSocketTransport from "@cubejs-client/ws-transport"
114+
115+
const cubeApi = cube({
116+
transport: new WebSocketTransport({
117+
authorization: CUBE_TOKEN,
118+
apiUrl: "ws://localhost:4000/"
119+
})
120+
})
121+
```
122+
123+
<ReferenceBox>
124+
125+
See [this recipe][ref-recipe-real-time-data-fetch] for an example of real-time
126+
data fetch using the WebSocket transport.
127+
128+
</ReferenceBox>
129+
91130
## Configuration
92131

93132
REST API is enabled by default and secured using [API scopes][self-api-scopes]
@@ -188,30 +227,7 @@ how to generate it.
188227
In the development environment the token is not required for authorization, but
189228
you can still use it to pass a security context.
190229

191-
### Continue wait
192-
193-
If the request takes too long to be processed, Cube Backend responds with
194-
`{ "error": "Continue wait" }` and 200 status code. This is how the long polling
195-
mechanism in Cube is implemented. Clients should continuously retry the same
196-
query in a loop until they get a successful result. Subsequent calls to the Cube
197-
endpoints are idempotent and don't lead to scheduling new database queries if
198-
not required by the [`refresh_key`][ref-schema-ref-cube-refresh-key]. Also,
199-
receiving `Continue wait` doesn't mean the database query has been canceled, and
200-
it's actually still being processed by the Cube. Database queries that weren't
201-
started and are no longer waited by the client's long polling loop will be
202-
marked as orphaned and removed from the querying queue.
203-
204-
Possible reasons of **Continue wait**:
205-
206-
- The query requested is heavy, and it takes some time for the database to
207-
process it. Clients should wait for its completion, continuously sending the
208-
same REST API request. [`continueWaitTimeout`][ref-conf-queue-opts] can be
209-
adjusted in order to change the time Cube waits before returning
210-
`Continue wait` message.
211-
- There are many queries requested and Cube backend queues them to save database
212-
from overloading.
213-
214-
### Error Handling
230+
### Error handling
215231

216232
Cube REST API has basic errors and HTTP Error codes for all requests.
217233

@@ -222,7 +238,7 @@ Cube REST API has basic errors and HTTP Error codes for all requests.
222238
| 403 | Invalid token | The auth token provided is not valid. It may be expired or have invalid signature. |
223239
| 500 | Error message | Cube internal server error. Check error message for details. |
224240

225-
### Request Span Annotation
241+
### Request span annotation
226242

227243
For monitoring tools such as Cube Cloud proper request span annotation should be
228244
provided in `x-request-id` header of a request. Each request id should consist
@@ -232,27 +248,31 @@ should be unique for each separate request. `spanId` should define user
232248
interaction span such us `Continue wait` retry cycle and it's value shouldn't
233249
change during one single interaction.
234250

235-
### Pagination
236-
237-
Cube supports paginated requests for the `/v1/load` endpoint by including
238-
[`limit` and `offset` parameters][ref-rest-query-format] in the query. For
239-
example, the following query will retrieve rows 101-200 from the `Orders` cube:
240-
241-
```json
242-
{
243-
"dimensions": ["Orders.status"],
244-
"measures": ["Orders.count"],
245-
"timeDimensions": [
246-
{
247-
"dimension": "Orders.createdAt",
248-
"dateRange": "last year",
249-
"granularity": "day"
250-
}
251-
],
252-
"limit": 100,
253-
"offset": 100
254-
}
255-
```
251+
## Troubleshooting
252+
253+
### `Continue wait`
254+
255+
If the request takes too long to be processed, the REST API responds with
256+
`{ "error": "Continue wait" }` and the status code 200.
257+
258+
This is part of the long polling mechanism:
259+
- After an API instance receives a request, it adds the query to the query queue.
260+
- If the query takes too long to be processed, the API instance will respond with `Continue wait`. Receiving `Continue wait` doesn't mean the database query has been canceled, and it's actually still being processed by Cube.
261+
- Clients who received `Continue wait` should continuously retry the same query in a loop until they get a successful result. Database queries that are no longer retried by clients will be marked as orphaned and removed from the query queue. Subsequent calls to the REST API are idempotent and don't lead to scheduling new database queries if not required by the [`refresh_key`][ref-schema-ref-cube-refresh-key].
262+
263+
Possible reasons of `Continue wait`:
264+
- The query is too heavy for the upstream database, i.e., it takes too long to be processed.
265+
- The maximum [concurrency][ref-concurrency] is reached, i.e., there are many queries
266+
waiting in the query queue until the previous ones are completed.
267+
268+
[`continueWaitTimeout`][ref-conf-queue-opts] configuration option can be adjusted
269+
in order to change the time Cube waits before returning `Continue wait` message.
270+
However, it's recommended to address the root cause of the issue instead of
271+
increasing the timeout:
272+
- Switch from a [traditional database][ref-traditional-databases] to a [data
273+
warehouse][ref-data-warehouses].
274+
- Increase the [concurrency][ref-concurrency] if your database can handle it.
275+
- Implement [pre-aggregations][ref-pre-aggregations] to reduce the query time by using Cube Store.
256276

257277
[mdn-cors]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
258278
[ref-config-js]: /product/configuration/reference/config
@@ -286,4 +306,11 @@ example, the following query will retrieve rows 101-200 from the `Orders` cube:
286306
[self-cors]: #configuration-cors
287307
[ref-ref-rest-api]: /product/apis-integrations/rest-api/reference
288308
[link-jq-utility]: https://jqlang.github.io/jq/
289-
[gh-cube-openspec]: https://github.com/cube-js/cube/blob/master/packages/cubejs-api-gateway/openspec.yml
309+
[gh-cube-openspec]: https://github.com/cube-js/cube/blob/master/packages/cubejs-api-gateway/openspec.yml
310+
[link-websocket]: https://en.wikipedia.org/wiki/WebSocket
311+
[ref-concurrency]: /product/configuration/concurrency
312+
[ref-data-warehouses]: /product/configuration/data-sources#data-warehouses
313+
[ref-traditional-databases]: /product/configuration/data-sources#transactional-databases
314+
[ref-pre-aggregations]: /product/caching/using-pre-aggregations
315+
[ref-javascript-sdk]: /product/apis-integrations/javascript-sdk
316+
[ref-recipe-real-time-data-fetch]: /product/apis-integrations/recipes/real-time-data-fetch
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module.exports = {
22
"query-format": "Query format",
3-
"real-time-data-fetch": "Real-Time data fetch",
43
"reference": "Reference"
54
}

docs/pages/product/apis-integrations/rest-api/real-time-data-fetch.mdx

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)