### Dart ZMQ Flutter Example Source: https://pub.dev/packages/dartzmq/example This is the main example for the dartzmq package, demonstrating how to set up a ZeroMQ socket in a Flutter application to send and receive messages. It includes important notes on platform-specific library placement and simulator configurations. ```dart import 'dart:async'; import 'dart:developer'; import 'package:dartzmq/dartzmq.dart'; import 'package:flutter/material.dart'; /// !IMPORTANT! If you are not running the example on Windows or Android /// dont't forget to copy your shared library (.dll, .so or .dylib) to the executable path /// !IMPORTANT! For IOS running on Simulator you have to replace the libzmq.a with libzmq_simulator.a in dartzmq.podspec file in the ios folder of plugin void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { final ZContext _context = ZContext(); late final MonitoredZSocket _socket; String _receivedData = ''; late StreamSubscription _subscription; int _presses = 0; @override void initState() { _socket = _context.createMonitoredSocket(SocketType.dealer); _socket.connect("tcp://localhost:5566"); // host ip address in android simulator is 10.0.2.2 // _socket.connect("tcp://10.0.2.2:5566"); // _socket.connect("tcp://192.168.2.34:5566"); // listen for messages _subscription = _socket.messages.listen((message) { setState(() { _receivedData = message.toString(); }); }); // listen for frames // _subscription = _socket.frames.listen((frame) { // setState(() { // _receivedData = frame.toString(); // }); // }); // listen for payloads // _subscription = _socket.payloads.listen((payload) { // setState(() { // _receivedData = payload.toString(); // }); // }); super.initState(); } @override void dispose() { _socket.close(); _context.stop(); _subscription.cancel(); super.dispose(); } void _sendMessage() { ++_presses; _socket.send([_presses], flags: ZMQ_DONTWAIT); // NOTE: if you're using dealer/rep, an empty message for identification is required. // _socket.send([], flags: ZMQ_DONTWAIT | ZMQ_SNDMORE); // _socket.send([_presses], flags: ZMQ_DONTWAIT); // // or you can use ZFrame to build an message: // // var newMessage = ZMessage(); // newMessage.add(ZFrame(Uint8List(0))); // newMessage.add(ZFrame(Uint8List.fromList([_presses]))); // _socket.sendMessage(newMessage, flags: ZMQ_DONTWAIT); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("dartzmq demo"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('Press to send a message'), MaterialButton( onPressed: _sendMessage, color: Colors.blue, child: const Text('Send'), ), StreamBuilder( stream: _socket.events, builder: (context, snapshot) { if (snapshot.hasData) { final event = snapshot.data!; log('Socket event: ${event.event}, value: ${event.value}'); return Text('Event: ${event.event}, value: ${event.value}'); } return const LinearProgressIndicator(); }, ), const Text('Received'), Text(_receivedData), ], ), ), ); } } ``` -------------------------------- ### Create ZContext Source: https://pub.dev/packages/dartzmq Initialize the ZeroMQ context. ```dart final ZContext context = ZContext(); ``` -------------------------------- ### Import dartzmq library Source: https://pub.dev/packages/dartzmq/install Include the package in your Dart files to access ZeroMQ functionality. ```dart import 'package:dartzmq/dartzmq.dart'; ``` -------------------------------- ### Add dartzmq dependency Source: https://pub.dev/packages/dartzmq/install Use the command line or pubspec.yaml to add the package to your project. ```bash $ flutter pub add dartzmq ``` ```yaml dependencies: dartzmq: ^1.0.0-dev.17 ``` -------------------------------- ### Create Sockets Source: https://pub.dev/packages/dartzmq Create either asynchronous or synchronous sockets using the ZContext. ```dart final ZSocket socket = context.createSocket(SocketType.req); ``` ```dart final ZSyncSocket socket = context.createSocket(SocketType.req); ``` -------------------------------- ### Socket Management and Messaging Source: https://pub.dev/packages/dartzmq/changelog Methods for creating ZeroMQ sockets, establishing connections, and sending data. ```APIDOC ## Socket Operations ### Description Methods to initialize and manage ZeroMQ sockets and communication. ### Methods - **Creating Sockets**: Supports pair, pub, sub, req, rep, dealer, router, pull, push, xPub, xSub, stream. - **bind(String address)**: Binds the socket to a local address. - **connect(String address)**: Connects the socket to a remote address. - **send(List message)**: Sends a message of type List. ### Security - **setCurvePublicKey(String key)**: Sets the CURVE public key. - **setCurveSecretKey(String key)**: Sets the CURVE secret key. - **setCurveServerKey(String key)**: Sets the CURVE server key. ### Configuration - **setOption(int option, String value)**: Sets specific socket options. ``` -------------------------------- ### Connect Socket Source: https://pub.dev/packages/dartzmq Connect a socket to a specific address. ```dart socket.connect("tcp://localhost:5566"); ``` -------------------------------- ### Configure iOS Podspec for Simulator Source: https://pub.dev/packages/dartzmq Modify the podspec file to link against the simulator-specific libzmq library. ```ruby s.pod_target_xcconfig = { "OTHER_LDFLAGS" => "$(inherited) -force_load $(PODS_TARGET_SRCROOT)/Frameworks/libzmq.a -lstdc++" } ``` ```ruby s.pod_target_xcconfig = { "OTHER_LDFLAGS" => "$(inherited) -force_load $(PODS_TARGET_SRCROOT)/Frameworks/libzmq_simulator.a -lstdc++" } ``` -------------------------------- ### Destroy Resources Source: https://pub.dev/packages/dartzmq Close sockets, monitors, and the context to release resources. ```dart socket.close(); ``` ```dart monitor.close(); ``` ```dart context.stop(); ``` -------------------------------- ### Receive Data Source: https://pub.dev/packages/dartzmq Listen for incoming messages, frames, or raw payloads. ```dart socket.messages.listen((message) { // Do something with message }); ``` ```dart socket.frames.listen((frame) { // Do something with frame }); ``` ```dart socket.payloads.listen((payload) { // Do something with payload }); ``` -------------------------------- ### Monitor Socket Events Source: https://pub.dev/packages/dartzmq Monitor socket state changes using MonitoredZSocket or ZMonitor. ```dart final MonitoredZSocket socket = context.createMonitoredSocket(SocketType.req); socket.events.listen((event) { log('Received event ${event.event} with value ${event.value}'); }); ``` ```dart final ZSocket socket = context.createSocket(SocketType.req); final ZMonitor monitor = ZMonitor( context: context, socket: socket, event: ZMQ_EVENT_CONNECTED | ZMQ_EVENT_CLOSED, // Only listen for connected and closed events ); monitor.events.listen((event) { log('Received event ${event.event} with value ${event.value}'); }); ``` -------------------------------- ### Send Messages Source: https://pub.dev/packages/dartzmq Send data using various formats including lists, strings, frames, or multipart messages. ```dart socket.send([1, 2, 3, 4, 5]); socket.sendString('My Message'); socket.sendFrame(ZFrame([1, 2, 3, 4, 5])); var message = ZMessage(); message.add(ZFrame([1, 2, 3, 4, 5])); message.add(ZFrame([6, 7, 8, 9, 10])); socket.sendMessage(message, flags: ZMQ_DONTWAIT); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.