blob: c087440a1802e58d542b89c152c7fbde7c3797b6 [file] [log] [blame] [view]
henrique5b789582014-03-15 23:11:34 +01001Thrift Node.js Library
2=========================
T Jake Luciani0c124bb2011-01-08 03:49:16 +00003
henrique5b789582014-03-15 23:11:34 +01004License
5-------
6Licensed to the Apache Software Foundation (ASF) under one
7or more contributor license agreements. See the NOTICE file
8distributed with this work for additional information
9regarding copyright ownership. The ASF licenses this file
10to you under the Apache License, Version 2.0 (the
11"License"); you may not use this file except in compliance
12with the License. You may obtain a copy of the License at
T Jake Luciani0c124bb2011-01-08 03:49:16 +000013
henrique5b789582014-03-15 23:11:34 +010014 http://www.apache.org/licenses/LICENSE-2.0
15
16Unless required by applicable law or agreed to in writing,
17software distributed under the License is distributed on an
18"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19KIND, either express or implied. See the License for the
20specific language governing permissions and limitations
21under the License.
22
James E. King, III699b5bc2017-09-14 08:07:08 -070023## Compatibility
24
James E. King IIIa37feaf2018-03-06 15:11:01 -050025node version 6 or later is required
T Jake Luciani0c124bb2011-01-08 03:49:16 +000026
27## Install
28
29 npm install thrift
30
31## Thrift Compiler
32
raa2e4e562014-03-29 01:14:48 -070033You can compile IDL sources for Node.js with the following command:
T Jake Luciani0c124bb2011-01-08 03:49:16 +000034
35 thrift --gen js:node thrift_file
36
37## Cassandra Client Example:
38
39Here is a Cassandra example:
40
Filip Spiridonov0293c362018-04-17 20:21:30 -070041```js
42var thrift = require('thrift'),
43 Cassandra = require('./gen-nodejs/Cassandra')
44 ttypes = require('./gen-nodejs/cassandra_types');
T Jake Luciani0c124bb2011-01-08 03:49:16 +000045
Filip Spiridonov0293c362018-04-17 20:21:30 -070046var connection = thrift.createConnection("localhost", 9160),
47 client = thrift.createClient(Cassandra, connection);
T Jake Luciani0c124bb2011-01-08 03:49:16 +000048
Filip Spiridonov0293c362018-04-17 20:21:30 -070049connection.on('error', function(err) {
50 console.error(err);
51});
T Jake Luciani0c124bb2011-01-08 03:49:16 +000052
Filip Spiridonov0293c362018-04-17 20:21:30 -070053client.get_slice("Keyspace", "key", new ttypes.ColumnParent({column_family: "ExampleCF"}), new ttypes.SlicePredicate({slice_range: new ttypes.SliceRange({start: '', finish: ''})}), ttypes.ConsistencyLevel.ONE, function(err, data) {
54 if (err) {
55 // handle err
56 } else {
57 // data == [ttypes.ColumnOrSuperColumn, ...]
58 }
59 connection.end();
60});
61```
T Jake Luciani0c124bb2011-01-08 03:49:16 +000062
Henrique Mendoncad407b2d2012-10-22 21:06:14 +000063<a name="int64"></a>
64## Int64
65
Jens Geyerfa133622016-12-26 11:27:24 +010066Since JavaScript represents all numbers as doubles, int64 values cannot be accurately represented naturally. To solve this, int64 values in responses will be wrapped with Thrift.Int64 objects. The Int64 implementation used is [broofa/node-int64](https://github.com/broofa/node-int64).
Henrique Mendoncad407b2d2012-10-22 21:06:14 +000067
Randy Abernethya7270072015-02-04 13:18:53 -080068## Client and server examples
Henrique Mendoncad407b2d2012-10-22 21:06:14 +000069
Randy Abernethya7270072015-02-04 13:18:53 -080070Several example clients and servers are included in the thrift/lib/nodejs/examples folder and the cross language tutorial thrift/tutorial/nodejs folder.
HIRANO Satoshi80984282019-10-09 07:06:30 +090071
72## Use on browsers
73
74You can use code generated with js:node on browsers with Webpack. Here is an example.
75
76thrift --gen js:node,ts,es6,with_ns
77
Eugen30ac2592020-01-07 15:28:45 +010078```javascript
79import * as thrift from 'thrift';
HIRANO Satoshi80984282019-10-09 07:06:30 +090080import { MyServiceClient } from '../gen-nodejs/MyService';
81
82let host = window.location.hostname;
83let port = 443;
84let opts = {
85 transport: thrift.TBufferedTransport,
86 protocol: thrift.TJSONProtocol,
87 headers: {
88 'Content-Type': 'application/vnd.apache.thrift.json',
89 },
90 https: true,
91 path: '/url/path',
92 useCORS: true,
93};
94
95let connection = thrift.createXHRConnection(host, port, opts);
96let thriftClient = thrift.createXHRClient(MyServiceClient, connection);
97
98connection.on('error', (err) => {
99 console.error(err);
100});
101
102thriftClient.myService(param)
103 .then((result) => {
104 console.log(result);
105 })
106 .catch((err) => {
107 ....
108 });
109```
110
Eugen30ac2592020-01-07 15:28:45 +0100111Bundlers, like webpack, will use thrift/browser.js by default because of the
112`"browser": "./lib/nodejs/lib/thrift/browser.js"` field in package.json.
113
114### Browser example with WebSocket, BufferedTransport and BinaryProtocol
115```javascript
116import thrift from 'thrift';
117import { MyServiceClient } from '../gen-nodejs/MyService';
118
119const host = window.location.hostname;
120const port = 9090;
121const opts = {
122 transport: thrift.TBufferedTransport,
123 protocol: thrift.TBinaryProtocol
124}
125const connection = thrift.createWSConnection(host, port, opts);
126connection.open();
127const thriftClient = thrift.createWSClient(MyServiceClient, connection);
128
129connection.on('error', (err) => {
130 console.error(err);
131});
132
133thriftClient.myService(param)
134 .then((result) => {
135 console.log(result);
136 })
137 .catch((err) => {
138 ....
139 });
140```
141
142