or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

client-proxies.mdcontext-objects.mdexceptions.mdgrpc.mdindex.mdmessage-patterns.mdmodules.mdtransports.md

transports.mddocs/

0

# Transport Implementations

1

2

Support for multiple messaging protocols including TCP, Redis, NATS, MQTT, gRPC, RabbitMQ, and Kafka with protocol-specific features and configurations for building distributed microservice architectures.

3

4

## Capabilities

5

6

### Transport Enum

7

8

Enumeration of available transport types for microservice communication.

9

10

```typescript { .api }

11

/**

12

* Available transport types for microservice communication

13

*/

14

enum Transport {

15

TCP = 0,

16

REDIS = 1,

17

NATS = 2,

18

MQTT = 3,

19

GRPC = 4,

20

RMQ = 5,

21

KAFKA = 6

22

}

23

```

24

25

### Microservice Configuration

26

27

Configuration interface for setting up microservice transport options.

28

29

```typescript { .api }

30

/**

31

* Microservice configuration options

32

*/

33

interface MicroserviceOptions {

34

/** Transport type or custom transport strategy */

35

transport?: Transport | CustomTransportStrategy;

36

/** Transport-specific configuration options */

37

options?: any;

38

}

39

40

/**

41

* Custom transport strategy interface

42

*/

43

interface CustomTransportStrategy {

44

server: any;

45

client: any;

46

}

47

```

48

49

**Usage Examples:**

50

51

```typescript

52

import { NestFactory } from '@nestjs/core';

53

import { Transport, MicroserviceOptions } from '@nestjs/microservices';

54

55

// TCP microservice

56

const tcpApp = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {

57

transport: Transport.TCP,

58

options: {

59

host: '127.0.0.1',

60

port: 3001,

61

},

62

});

63

64

// Redis microservice

65

const redisApp = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {

66

transport: Transport.REDIS,

67

options: {

68

host: 'localhost',

69

port: 6379,

70

retryAttempts: 5,

71

retryDelay: 3000,

72

},

73

});

74

```

75

76

### TCP Transport

77

78

Simple, reliable TCP socket-based communication for high-performance local networks.

79

80

```typescript { .api }

81

/**

82

* TCP transport server implementation

83

*/

84

class ServerTCP extends Server {

85

constructor(options: TcpOptions);

86

}

87

88

/**

89

* TCP transport client implementation

90

*/

91

class ClientTCP extends ClientProxy {

92

constructor(options: TcpOptions);

93

}

94

95

interface TcpOptions {

96

/** Server host address */

97

host?: string;

98

/** Server port number */

99

port?: number;

100

/** Custom socket class */

101

socketClass?: any;

102

/** TLS encryption options */

103

tlsOptions?: any;

104

}

105

```

106

107

**Usage Examples:**

108

109

```typescript

110

// TCP Server

111

const app = await NestFactory.createMicroservice(AppModule, {

112

transport: Transport.TCP,

113

options: {

114

host: '0.0.0.0',

115

port: 3001,

116

tlsOptions: {

117

key: fs.readFileSync('server-key.pem'),

118

cert: fs.readFileSync('server-cert.pem'),

119

},

120

},

121

});

122

123

// TCP Client

124

const client = ClientProxyFactory.create({

125

transport: Transport.TCP,

126

options: {

127

host: '127.0.0.1',

128

port: 3001,

129

},

130

});

131

```

132

133

### Redis Transport

134

135

Redis pub/sub based transport for scalable message broadcasting and caching integration.

136

137

```typescript { .api }

138

/**

139

* Redis transport server implementation

140

*/

141

class ServerRedis extends Server {

142

constructor(options: RedisOptions);

143

}

144

145

/**

146

* Redis transport client implementation

147

*/

148

class ClientRedis extends ClientProxy {

149

constructor(options: RedisOptions);

150

getRequestPattern(pattern: string): string;

151

getReplyPattern(pattern: string): string;

152

}

153

154

interface RedisOptions {

155

/** Redis server host */

156

host?: string;

157

/** Redis server port */

158

port?: number;

159

/** Redis password */

160

password?: string;

161

/** Redis database number */

162

db?: number;

163

/** Connection retry attempts */

164

retryAttempts?: number;

165

/** Retry delay in milliseconds */

166

retryDelay?: number;

167

}

168

```

169

170

### NATS Transport

171

172

Lightweight, high-performance messaging system with subject-based routing.

173

174

```typescript { .api }

175

/**

176

* NATS transport server implementation

177

*/

178

class ServerNats extends Server {

179

constructor(options: NatsOptions);

180

}

181

182

/**

183

* NATS transport client implementation

184

*/

185

class ClientNats extends ClientProxy {

186

constructor(options: NatsOptions);

187

createClient(): Promise<any>;

188

}

189

190

interface NatsOptions {

191

/** NATS server URLs */

192

servers?: string[];

193

/** Client name */

194

name?: string;

195

/** Authentication user */

196

user?: string;

197

/** Authentication password */

198

pass?: string;

199

/** Connection timeout */

200

timeout?: number;

201

}

202

```

203

204

### MQTT Transport

205

206

Lightweight publish-subscribe messaging protocol ideal for IoT and mobile applications.

207

208

```typescript { .api }

209

/**

210

* MQTT transport server implementation

211

*/

212

class ServerMqtt extends Server {

213

constructor(options: MqttOptions);

214

}

215

216

/**

217

* MQTT transport client implementation

218

*/

219

class ClientMqtt extends ClientProxy {

220

constructor(options: MqttOptions);

221

getRequestPattern(pattern: string): string;

222

getResponsePattern(pattern: string): string;

223

}

224

225

interface MqttOptions {

226

/** MQTT broker URL */

227

url?: string;

228

/** Client identifier */

229

clientId?: string;

230

/** Authentication username */

231

username?: string;

232

/** Authentication password */

233

password?: string;

234

/** Quality of service level */

235

qos?: 0 | 1 | 2;

236

/** Retain messages flag */

237

retain?: boolean;

238

}

239

```

240

241

### gRPC Transport

242

243

High-performance RPC framework with protocol buffer serialization and streaming support.

244

245

```typescript { .api }

246

/**

247

* gRPC transport server implementation

248

*/

249

class ServerGrpc extends Server {

250

constructor(options: GrpcOptions);

251

}

252

253

/**

254

* gRPC transport client implementation

255

*/

256

class ClientGrpcProxy extends ClientProxy implements ClientGrpc {

257

constructor(options: GrpcOptions);

258

getService<T>(name: string): T;

259

getClientByServiceName<T>(name: string): T;

260

}

261

262

interface GrpcOptions {

263

/** Protocol buffer package name */

264

package: string | string[];

265

/** Path to .proto files */

266

protoPath: string | string[];

267

/** gRPC server URL */

268

url?: string;

269

/** Proto loader options */

270

loader?: any;

271

/** gRPC credentials */

272

credentials?: any;

273

}

274

```

275

276

**Usage Examples:**

277

278

```typescript

279

// gRPC Server

280

const app = await NestFactory.createMicroservice(AppModule, {

281

transport: Transport.GRPC,

282

options: {

283

package: 'hero',

284

protoPath: path.join(__dirname, 'hero.proto'),

285

url: '0.0.0.0:5000',

286

loader: {

287

keepCase: true,

288

longs: String,

289

enums: String,

290

defaults: true,

291

oneofs: true,

292

},

293

},

294

});

295

296

// gRPC Client

297

const client = ClientProxyFactory.create({

298

transport: Transport.GRPC,

299

options: {

300

package: 'hero',

301

protoPath: path.join(__dirname, 'hero.proto'),

302

url: 'localhost:5000',

303

},

304

}) as ClientGrpc;

305

306

const heroService = client.getService<HeroService>('HeroService');

307

```

308

309

### RabbitMQ Transport

310

311

Advanced message queuing with routing, durability, and complex messaging patterns.

312

313

```typescript { .api }

314

/**

315

* RabbitMQ transport server implementation

316

*/

317

class ServerRMQ extends Server {

318

constructor(options: RmqOptions);

319

}

320

321

/**

322

* RabbitMQ transport client implementation

323

*/

324

class ClientRMQ extends ClientProxy {

325

constructor(options: RmqOptions);

326

createChannel(): Promise<void>;

327

}

328

329

interface RmqOptions {

330

/** RabbitMQ connection URLs */

331

urls?: string[];

332

/** Default queue name */

333

queue?: string;

334

/** Queue declaration options */

335

queueOptions?: any;

336

/** Prefetch count for consumers */

337

prefetchCount?: number;

338

/** Channel setup callback */

339

socketOptions?: any;

340

}

341

```

342

343

### Kafka Transport

344

345

High-throughput, distributed streaming platform with partitioning and replication.

346

347

```typescript { .api }

348

/**

349

* Kafka transport server implementation

350

*/

351

class ServerKafka extends Server {

352

constructor(options: KafkaOptions);

353

}

354

355

/**

356

* Kafka transport client implementation

357

*/

358

class ClientKafka extends ClientProxy {

359

constructor(options: KafkaOptions);

360

subscribeToResponseOf(pattern: unknown): void;

361

emitBatch<TResult, TInput>(

362

pattern: any,

363

data: { messages: TInput[] }

364

): Observable<TResult>;

365

commitOffsets(topicPartitions: TopicPartitionOffsetAndMetadata[]): Promise<void>;

366

}

367

368

interface KafkaOptions {

369

/** Kafka client configuration */

370

client: {

371

clientId: string;

372

brokers: string[];

373

ssl?: any;

374

sasl?: any;

375

};

376

/** Consumer group configuration */

377

consumer?: {

378

groupId: string;

379

allowAutoTopicCreation?: boolean;

380

sessionTimeout?: number;

381

heartbeatInterval?: number;

382

};

383

/** Producer configuration */

384

producer?: {

385

allowAutoTopicCreation?: boolean;

386

transactionTimeout?: number;

387

maxInFlightRequests?: number;

388

};

389

}

390

```

391

392

**Usage Examples:**

393

394

```typescript

395

// Kafka Server

396

const app = await NestFactory.createMicroservice(AppModule, {

397

transport: Transport.KAFKA,

398

options: {

399

client: {

400

clientId: 'hero-server',

401

brokers: ['localhost:9092'],

402

},

403

consumer: {

404

groupId: 'hero-consumer',

405

},

406

},

407

});

408

409

// Kafka Client with batch operations

410

const kafkaClient = ClientProxyFactory.create({

411

transport: Transport.KAFKA,

412

options: {

413

client: {

414

clientId: 'hero-client',

415

brokers: ['localhost:9092'],

416

},

417

},

418

});

419

420

// Send batch of messages

421

await kafkaClient.emitBatch('user.events', {

422

messages: [

423

{ key: 'user1', value: { action: 'login', timestamp: Date.now() } },

424

{ key: 'user2', value: { action: 'logout', timestamp: Date.now() } },

425

],

426

}).toPromise();

427

```

428

429

### Server Base Classes

430

431

Base server implementations for transport-specific servers.

432

433

```typescript { .api }

434

/**

435

* Base server class for all transport implementations

436

*/

437

abstract class Server {

438

abstract listen(callback: () => void): void;

439

abstract close(): void;

440

}

441

442

/**

443

* Server factory for creating transport-specific servers

444

*/

445

class ServerFactory {

446

static create(

447

microserviceOptions: MicroserviceOptions

448

): Server & CustomTransportStrategy;

449

}

450

```