import 'dart:convert'; import 'package:dart_amqp/dart_amqp.dart'; class RabbitBridge { final String username; final void Function(String rk, Map body) onMsg; RabbitBridge({required this.username, required this.onMsg}); late final Client _client; late final Channel _ch; Future connect() async { // 1) open connection + channel _client = Client( settings: ConnectionSettings( host: "rabbitmq-wanis.wanis.io", port: 5672, virtualHost: "/wanis", authProvider: const PlainAuthenticator("wanis", "wanisSecret"), ), ); _ch = await _client.channel(); // 2) get / create the topic exchange declared by Spring final Exchange ex = await _ch.exchange( "socket.bridge", ExchangeType.TOPIC, durable: true, ); // 3) declare a fresh, exclusive queue for this app session final Queue q = await _ch.queue("", exclusive: true); // receive everything addressed to *this* user await q.bind(ex, "user.$username.#"); // (optional) also receive global broadcasts await q.bind(ex, "broadcast.#"); // 4) start consuming final Consumer consumer = await q.consume(); consumer.listen((AmqpMessage m) { final payload = jsonDecode(utf8.decode(m.payload as List)) as Map; onMsg(m.routingKey!, payload); }); } Future dispose() => _client.close(); }