expo-mqtt
v0.1.4
Published
High-performance MQTT and AMQP (RabbitMQ) client for Expo and React Native.
Downloads
348
Maintainers
Readme
expo-mqtt
A high-performance MQTT and AMQP (RabbitMQ) client for Expo and React Native. This library provides a unified native interface for both protocols, optimized for modern mobile applications.
Features
- 🔄 Unified API: Easy-to-use interfaces for both MQTT and AMQP.
- 🚀 Native Performance: Built using the Expo Modules API for maximum efficiency.
- 📱 Cross-Platform: Supports Android, iOS, and Web.
- 🔔 Event-Driven: Full support for connection, disconnection, and message events.
Installation
Managed Expo projects
npx expo install expo-mqttBare React Native projects
Ensure you have installed and configured the expo package first, then run:
npx expo install expo-mqttAndroid
No additional configuration required.
iOS
Run npx pod-install after installation.
Usage
Unified Client
import ExpoMqtt from 'expo-mqtt';
// MQTT usage (host, port, clientId, username?, password?)
await ExpoMqtt.mqttConnect('broker.hivemq.com', 1883, 'my-client-id', 'user', 'pass');
await ExpoMqtt.mqttPublish('expo/test', 'Hello MQTT!');
// AMQP usage (url, username?, password?)
// Tip: Use a clean URL like 'amqp://localhost:5672' and pass credentials separately
// to avoid URI encoding issues with special characters.
await ExpoMqtt.amqpConnect('amqp://192.168.1.1:5672', 'targino', 'password');
await ExpoMqtt.amqpPublish('amq.direct', 'routing-key', 'Hello RMQ!');API Reference
MQTT Functions
mqttConnect(host: string, port: number, clientId: string, username?: string, password?: string): Promise<void>mqttDisconnect(): Promise<void>mqttPublish(topic: string, message: string): Promise<void>mqttSubscribe(topic: string): Promise<void>
AMQP Functions
amqpConnect(url: string, username?: string, password?: string): Promise<void>amqpDisconnect(): Promise<void>amqpPublish(exchange: string, routingKey: string, message: string, type?: ExchangeType): Promise<void>amqpDeclareExchange(name: string, type: ExchangeType): Promise<void>amqpConsume(queue: string): Promise<void>
Types
type ExchangeType = 'direct' | 'fanout' | 'topic' | 'headers';Development
Unit Tests
The library includes a suite of unit tests using Jest.
npm run testExample App Cleanup
To prevent sensitive credentials from being committed in the example app, we use a cleanup script:
node scripts/cleanup-example.jsNote: A git pre-commit hook is automatically installed to run this script before every commit.
Contributing
Contributions are very welcome! Please refer to our contributing guide.
License
MIT © Rafael Targino
