Node.js bindings for Apache Thrift RPC system providing client/server libraries, protocols, and transports for cross-language service communication.
npx @tessl/cli install tessl/npm-thrift@0.22.0Node.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.
npm install thriftconst thrift = require('thrift');
// Common pattern - destructure needed components
const {
createConnection,
createClient,
createServer,
TBinaryProtocol,
TBufferedTransport
} = require('thrift');For ES modules:
import thrift from 'thrift';
import { createConnection, createClient } from 'thrift';const thrift = require('thrift');
const MyService = require('./gen-nodejs/MyService');
// Create connection
const connection = thrift.createConnection('localhost', 9090, {
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol
});
// Handle connection events
connection.on('error', (err) => {
console.error('Connection error:', err);
});
// Create client
const client = thrift.createClient(MyService, connection);
// Call remote methods
client.myMethod('hello world', (err, result) => {
if (err) {
console.error('RPC error:', err);
} else {
console.log('Result:', result);
}
});const thrift = require('thrift');
const MyService = require('./gen-nodejs/MyService');
// Implement service
const serviceHandler = {
myMethod: function(message, callback) {
console.log('Received:', message);
callback(null, 'Response: ' + message);
}
};
// Create server
const server = thrift.createServer(MyService, serviceHandler, {
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol
});
// Start server
server.listen(9090, () => {
console.log('Server listening on port 9090');
});The Node.js Thrift library provides a modular architecture with pluggable components:
Comprehensive connection management supporting multiple transport mechanisms including TCP, HTTP, WebSocket, and XHR connections with SSL/TLS support and automatic reconnection.
// TCP connections
function createConnection(host, port, options): Connection;
function createSSLConnection(host, port, options): Connection;
function createUDSConnection(path, options): Connection;
// HTTP connections
function createHttpConnection(host, port, options): HttpConnection;
function createHttpClient(ServiceClient, connection): Client;
// WebSocket connections
function createWSConnection(host, port, options): WSConnection;
function createWSClient(ServiceClient, connection): Client;Multiple serialization protocols for efficient data transmission with full JavaScript type support and configurable options for performance optimization.
// Protocol classes
class TBinaryProtocol {
constructor(transport, strictRead?, strictWrite?);
}
class TCompactProtocol {
constructor(transport);
}
class TJSONProtocol {
constructor(transport);
}
class THeaderProtocol {
constructor(transport, clientType?);
}Flexible transport implementations providing buffering, framing, and WebSocket capabilities with seamless protocol integration and error handling.
// Transport classes
class TBufferedTransport {
constructor(buffer?, callback?);
isOpen(): boolean;
open(callback): void;
close(): void;
read(len): Buffer;
write(buf): void;
flush(): void;
}
class TFramedTransport {
constructor(buffer?, callback?);
// Same interface as TBufferedTransport
}Production-ready server implementations supporting single and multiplexed services with configurable protocols, transports, and comprehensive error handling.
// Server creation
function createServer(processor, handler, options): Server;
function createMultiplexServer(processor, options): MultiplexServer;
function createWebServer(processor, options): WebServer;
// Server interface
class Server {
listen(port, host?, callback?): void;
close(callback?): void;
on(event, listener): void;
}// Core Thrift types and constants
const Type = {
STOP: 0, VOID: 1, BOOL: 2, BYTE: 3, I08: 3,
DOUBLE: 4, I16: 6, I32: 8, I64: 10,
STRING: 11, UTF7: 11, STRUCT: 12, MAP: 13,
SET: 14, LIST: 15, UTF8: 16, UTF16: 17
};
const MessageType = {
CALL: 1, REPLY: 2, EXCEPTION: 3, ONEWAY: 4
};
// Exception classes
class TException extends Error {
constructor(message: string);
}
class TApplicationException extends TException {
constructor(type?: number, message?: string);
}
// Utility types
const Int64: typeof import('node-int64');
const Q: typeof import('q');