blob: ade9ae4f1ac1710b53771f1896117888919f89b3 [file] [log] [blame]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="en-us" http-equiv="Content-Language" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/images/favicon.ico" rel="shortcut icon" />
<link href="/static/css/style.css" rel="stylesheet" type="text/css" />
<link href="/static/css/codehilite.css" rel="stylesheet" type="text/css" />
<link href="/static/css/bootstrap.css" media="screen, projection" rel="stylesheet" type="text/css" />
<link href="/static/css/thrift.css" media="screen, projection" rel="stylesheet" type="text/css" />
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap-dropdown.js"></script>
<script src="/static/js/bootstrap-tab.js"></script>
<script src="/static/js/thrift.js"></script>
<title>Apache Thrift - node.js library</title>
</head>
<body>
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="/">Apache Thrift &trade;</a>
<div class="nav-collapse">
<ul class="nav pull-right">
<li><a href="/download">Download</a></li>
<li><a href="/docs">Documentation</a></li>
<li><a href="/developers">Developers</a></li>
<li><a href="/lib">Libraries</a></li>
<li><a href="/tutorial">Tutorial</a></li>
<li><a href="/test">Test Suite</a></li>
<li><a href="/about">About</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Apache <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="http://www.apache.org/" target="_blank">Apache Home</a></li>
<li><a href="http://www.apache.org/licenses/" target="_blank">Apache License v2.0</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html" target="_blank">Donate</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html" target="_blank">Thanks</a></li>
<li><a href="http://www.apache.org/security/" target="_blank">Security</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="container">
<h2 id="compatibility">Compatibility</h2>
<p>node version 6 or later is required</p>
<h2 id="install">Install</h2>
<pre><code>npm install thrift
</code></pre>
<h2 id="thrift-compiler">Thrift Compiler</h2>
<p>You can compile IDL sources for Node.js with the following command:</p>
<pre><code>thrift --gen js:node thrift_file
</code></pre>
<h2 id="cassandra-client-example">Cassandra Client Example:</h2>
<p>Here is a Cassandra example:</p>
<p>```js
var thrift = require(‘thrift’),
Cassandra = require(‘./gen-nodejs/Cassandra’)
ttypes = require(‘./gen-nodejs/cassandra_types’);</p>
<p>var connection = thrift.createConnection(“localhost”, 9160),
client = thrift.createClient(Cassandra, connection);</p>
<p>connection.on(‘error’, function(err) {
console.error(err);
});</p>
<p>client.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) {
if (err) {
// handle err
} else {
// data == [ttypes.ColumnOrSuperColumn, …]
}
connection.end();
});
```</p>
<p><a name="int64"></a>
## Int64</p>
<p>Since 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 <a href="https://github.com/broofa/node-int64">broofa/node-int64</a>.</p>
<h2 id="client-and-server-examples">Client and server examples</h2>
<p>Several example clients and servers are included in the thrift/lib/nodejs/examples folder and the cross language tutorial thrift/tutorial/nodejs folder.</p>
<h2 id="use-on-browsers">Use on browsers</h2>
<p>You can use code generated with js:node on browsers with Webpack. Here is an example.</p>
<p>thrift –gen js:node,ts,es6,with_ns</p>
<p>```javascript
import * as thrift from ‘thrift’;
import { MyServiceClient } from ‘../gen-nodejs/MyService’;</p>
<p>let host = window.location.hostname;
let port = 443;
let opts = {
transport: thrift.TBufferedTransport,
protocol: thrift.TJSONProtocol,
headers: {
‘Content-Type’: ‘application/vnd.apache.thrift.json’,
},
https: true,
path: ‘/url/path’,
useCORS: true,
};</p>
<p>let connection = thrift.createXHRConnection(host, port, opts);
let thriftClient = thrift.createXHRClient(MyServiceClient, connection);</p>
<p>connection.on(‘error’, (err) =&gt; {
console.error(err);
});</p>
<p>thriftClient.myService(param)
.then((result) =&gt; {
console.log(result);
})
.catch((err) =&gt; {
….
});
```</p>
<p>Bundlers, like webpack, will use thrift/browser.js by default because of the
<code>"browser": "./lib/nodejs/lib/thrift/browser.js"</code> field in package.json.</p>
<h3 id="browser-example-with-websocket-bufferedtransport-and-binaryprotocol">Browser example with WebSocket, BufferedTransport and BinaryProtocol</h3>
<p>```javascript
import thrift from ‘thrift’;
import { MyServiceClient } from ‘../gen-nodejs/MyService’;</p>
<p>const host = window.location.hostname;
const port = 9090;
const opts = {
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol
}
const connection = thrift.createWSConnection(host, port, opts);
connection.open();
const thriftClient = thrift.createWSClient(MyServiceClient, connection);</p>
<p>connection.on(‘error’, (err) =&gt; {
console.error(err);
});</p>
<p>thriftClient.myService(param)
.then((result) =&gt; {
console.log(result);
})
.catch((err) =&gt; {
….
});
```</p>
<p class="snippet_footer">This page was generated by Apache Thrift's <strong>source tree docs</strong>:
<a href="https://gitbox.apache.org/repos/asf?p=thrift.git;a=blob;hb=HEAD;f=lib/nodejs/README.md">lib/nodejs/README.md</a>
</p>
</div>
<div class="container">
<hr>
<footer class="footer">
<div class="row">
<div class="span3">
<h3>Links</h3>
<ul class="unstyled">
<li><a href="/download">Download</a></li>
<li><a href="/developers">Developers</a></li>
<li><a href="/tutorial">Tutorials</a></li>
</ul>
<ul class="unstyled">
<li><a href="/sitemap">Sitemap</a></li>
</ul>
</div>
<div class="span3">
<h3>Get Involved</h3>
<ul class="unstyled">
<li><a href="/mailing">Mailing Lists</a></li>
<li><a href="http://issues.apache.org/jira/browse/THRIFT">Issue Tracking</a></li>
<li><a href="/docs/HowToContribute">How To Contribute</a></li>
</ul>
</div>
<div class="span6">
<a href="http://www.apache.org/"><img src="/static/images/feather.svg" onerror="this.src='/static/images/feather.png';this.onerror=null;" /></a>
Copyright &copy; 2024 <a href="http://www.apache.org/">Apache Software Foundation</a>.
Licensed under the <a href="http://www.apache.org/licenses/">Apache License v2.0</a>.
Apache, Apache Thrift, and the Apache feather logo are trademarks of The Apache Software Foundation.
</div>
</div>
</footer>
</div>
</body>
</html>