Skip to content

edorgeville/trpc-mqtt

 
 

Repository files navigation

trpc-mqtt

trpc-mqtt



Usage

1. Install trpc-mqtt.

# npm
npm install trpc-mqtt mqtt@5
# yarn
yarn add trpc-mqtt mqtt@5
# pnpm
pnpm add trpc-mqtt mqtt@5

2. Use mqttLink in your client code.

import { createTRPCProxyClient } from '@trpc/client';
import { mqttLink } from 'trpc-mqtt/link';
import mqtt from 'mqtt';

import type { AppRouter } from './appRouter';

const client = mqtt.connect('mqtt://localhost');

export const trpc = createTRPCProxyClient<AppRouter>({
  links: [
    mqttLink({
      client,
      requestTopic: "rpc/request"
    })
  ],
});

Note: don't forget to clean up the MQTT client when you're done, using client.end().

3. Use createMQTTHandler to handle incoming calls via mqtt on the server.

import { createMQTTHandler } from 'trpc-mqtt/adapter';

import { appRouter } from './appRouter';

const client = mqtt.connect('mqtt://localhost');

createMQTTHandler({ 
  client,
  requestTopic: "rpc/request",
  router: appRouter
});

Note: same as with the link, don't forget to clean up the MQTT client when you're done, using client.end().

License

Distributed under the MIT License. See LICENSE for more information.

Special thanks

This project is a fork of trpc-rabbitmq by Piotr Adamczyk

Languages

  • TypeScript 91.2%
  • JavaScript 8.8%