0
# Thrift
1
2
Node.js bindings for Apache Thrift RPC system providing comprehensive client and server libraries for cross-language service communication. This package offers complete protocol implementations, flexible transport mechanisms, and robust server frameworks optimized for Node.js environments.
3
4
## Package Information
5
6
- **Package Name**: thrift
7
- **Package Type**: npm
8
- **Language**: JavaScript/Node.js
9
- **Installation**: `npm install thrift`
10
- **Version**: 0.22.0
11
- **License**: Apache-2.0
12
13
## Core Imports
14
15
```javascript
16
const thrift = require('thrift');
17
18
// Common pattern - destructure needed components
19
const {
20
createConnection,
21
createClient,
22
createServer,
23
TBinaryProtocol,
24
TBufferedTransport
25
} = require('thrift');
26
```
27
28
For ES modules:
29
```javascript
30
import thrift from 'thrift';
31
import { createConnection, createClient } from 'thrift';
32
```
33
34
## Basic Usage
35
36
### Client Example
37
38
```javascript
39
const thrift = require('thrift');
40
const MyService = require('./gen-nodejs/MyService');
41
42
// Create connection
43
const connection = thrift.createConnection('localhost', 9090, {
44
transport: thrift.TBufferedTransport,
45
protocol: thrift.TBinaryProtocol
46
});
47
48
// Handle connection events
49
connection.on('error', (err) => {
50
console.error('Connection error:', err);
51
});
52
53
// Create client
54
const client = thrift.createClient(MyService, connection);
55
56
// Call remote methods
57
client.myMethod('hello world', (err, result) => {
58
if (err) {
59
console.error('RPC error:', err);
60
} else {
61
console.log('Result:', result);
62
}
63
});
64
```
65
66
### Server Example
67
68
```javascript
69
const thrift = require('thrift');
70
const MyService = require('./gen-nodejs/MyService');
71
72
// Implement service
73
const serviceHandler = {
74
myMethod: function(message, callback) {
75
console.log('Received:', message);
76
callback(null, 'Response: ' + message);
77
}
78
};
79
80
// Create server
81
const server = thrift.createServer(MyService, serviceHandler, {
82
transport: thrift.TBufferedTransport,
83
protocol: thrift.TBinaryProtocol
84
});
85
86
// Start server
87
server.listen(9090, () => {
88
console.log('Server listening on port 9090');
89
});
90
```
91
92
## Architecture
93
94
The Node.js Thrift library provides a modular architecture with pluggable components:
95
96
- **Connection Layer**: Multiple connection types (TCP, HTTP, WebSocket, XHR) with configurable options
97
- **Protocol Layer**: Serialization formats (Binary, Compact, JSON, Header) for data encoding
98
- **Transport Layer**: Communication mechanisms (Buffered, Framed, WebSocket) with layering support
99
- **Server Layer**: Flexible server implementations (Simple, Multiplexed, Web) with event handling
100
- **Client Layer**: Auto-generated client stubs with callback-based and promise-based APIs
101
- **Multiplexing**: Support for multiple services over a single connection
102
103
## Capabilities
104
105
### Connection Management
106
107
Comprehensive connection management supporting multiple transport mechanisms including TCP, HTTP, WebSocket, and XHR connections with SSL/TLS support and automatic reconnection.
108
109
```javascript { .api }
110
// TCP connections
111
function createConnection(host, port, options): Connection;
112
function createSSLConnection(host, port, options): Connection;
113
function createUDSConnection(path, options): Connection;
114
115
// HTTP connections
116
function createHttpConnection(host, port, options): HttpConnection;
117
function createHttpClient(ServiceClient, connection): Client;
118
119
// WebSocket connections
120
function createWSConnection(host, port, options): WSConnection;
121
function createWSClient(ServiceClient, connection): Client;
122
```
123
124
[Connection Management](./connections.md)
125
126
### Protocol Support
127
128
Multiple serialization protocols for efficient data transmission with full JavaScript type support and configurable options for performance optimization.
129
130
```javascript { .api }
131
// Protocol classes
132
class TBinaryProtocol {
133
constructor(transport, strictRead?, strictWrite?);
134
}
135
136
class TCompactProtocol {
137
constructor(transport);
138
}
139
140
class TJSONProtocol {
141
constructor(transport);
142
}
143
144
class THeaderProtocol {
145
constructor(transport, clientType?);
146
}
147
```
148
149
[Protocol Support](./protocols.md)
150
151
### Transport Layer
152
153
Flexible transport implementations providing buffering, framing, and WebSocket capabilities with seamless protocol integration and error handling.
154
155
```javascript { .api }
156
// Transport classes
157
class TBufferedTransport {
158
constructor(buffer?, callback?);
159
isOpen(): boolean;
160
open(callback): void;
161
close(): void;
162
read(len): Buffer;
163
write(buf): void;
164
flush(): void;
165
}
166
167
class TFramedTransport {
168
constructor(buffer?, callback?);
169
// Same interface as TBufferedTransport
170
}
171
```
172
173
[Transport Layer](./transports.md)
174
175
### Server Framework
176
177
Production-ready server implementations supporting single and multiplexed services with configurable protocols, transports, and comprehensive error handling.
178
179
```javascript { .api }
180
// Server creation
181
function createServer(processor, handler, options): Server;
182
function createMultiplexServer(processor, options): MultiplexServer;
183
function createWebServer(processor, options): WebServer;
184
185
// Server interface
186
class Server {
187
listen(port, host?, callback?): void;
188
close(callback?): void;
189
on(event, listener): void;
190
}
191
```
192
193
[Server Framework](./servers.md)
194
195
## Types
196
197
```javascript { .api }
198
// Core Thrift types and constants
199
const Type = {
200
STOP: 0, VOID: 1, BOOL: 2, BYTE: 3, I08: 3,
201
DOUBLE: 4, I16: 6, I32: 8, I64: 10,
202
STRING: 11, UTF7: 11, STRUCT: 12, MAP: 13,
203
SET: 14, LIST: 15, UTF8: 16, UTF16: 17
204
};
205
206
const MessageType = {
207
CALL: 1, REPLY: 2, EXCEPTION: 3, ONEWAY: 4
208
};
209
210
// Exception classes
211
class TException extends Error {
212
constructor(message: string);
213
}
214
215
class TApplicationException extends TException {
216
constructor(type?: number, message?: string);
217
}
218
219
// Utility types
220
const Int64: typeof import('node-int64');
221
const Q: typeof import('q');
222
```