runStompExample function

Future<void> runStompExample()

Implementation

Future<void> runStompExample() async {
  // Create two hosts - one will be the server, one will be the client
  final serverHost = await createHost('server');
  final clientHost = await createHost('client');

  print('📡 Server Host ID: ${serverHost.id}');
  print('📱 Client Host ID: ${clientHost.id}');

  // Start the STOMP server
  print('\n🏗️  Setting up STOMP server...');
  final stompService = await serverHost.addStompService(
    options: const StompServiceOptions.serverEnabled(
      serverName: 'dart-libp2p-stomp-example/1.0',
    ),
  );

  print('✅ STOMP server started');

  // Give the server a moment to start
  await Future.delayed(const Duration(milliseconds: 100));

  // Connect client to server
  print('\n🔗 Connecting client to server...');
  final client = await clientHost.connectStomp(
    peerId: serverHost.id,
    hostName: 'example.com',
  );

  print('✅ Client connected to server');
  print('   Session ID: ${client.sessionId}');
  print('   Server Info: ${client.serverInfo}');

  // Example 1: Simple message sending
  await demonstrateSimpleMessaging(client);

  // Example 2: Subscriptions and message delivery
  await demonstrateSubscriptions(client);

  // Example 3: Transactions
  await demonstrateTransactions(client);

  // Example 4: Server-side message broadcasting
  await demonstrateServerBroadcast(stompService, client);

  // Clean up
  print('\n🧹 Cleaning up...');
  await client.close();
  await stompService.stop();
  await serverHost.close();
  await clientHost.close();

  print('✅ STOMP example completed successfully!');
}