A simple dart zeromq implementation/wrapper around the libzmq C++ library

Overview

dartzmq

A simple dart zeromq implementation/wrapper around the libzmq C++ library

Features

Currently supported:

  • Creating sockets (pair, pub, sub, req, rep, dealer, router, pull, push, xPub, xSub, stream)
  • Sending messages (of type List<int>)
  • Bind (bind(String address))
  • Connect (connect(String address))
  • Curve (setCurvePublicKey(String key), setCurveSecretKey(String key) and setCurveServerKey(String key))
  • Socket options (setOption(int option, String value))
  • Receiving multipart messages (ZMessage)
  • Topic subscription for sub sockets (subscribe(String topic) & unsubscribe(String topic))

Getting started

Currently Windows and Android are officially supported as platforms, but it depends on the used shared library of libzmq. I have tested this on Windows and Android, which work. Other platforms have not been tested, but should work. If you have tested this plugin on another platform and got it to work, please share your steps and create an issue so I can add it to the list below.

Windows

Place a shared library of libzmq next to your executable (for example place libzmq-v142-mt-4_3_5.dll in the folder yourproject/build/windows/runner/Debug/)

Note that in order for this plugin to work you will need to either get a shared library of libzmq or compile it yourself. Especially when using this on windows you need to make sure that libzmq is compiled using MSVC-2019 if you are using clang it will not work (more info)

Android

Note that you need to use Android NDK version r21d. Newer versions are currently not supported (see https://github.com/zeromq/libzmq/issues/4276)

  1. Follow these steps to build a libzmq.so for different platforms
    • If you need curve support make sure to set the environment variable CURVE either to export CURVE=libsodium or export CURVE=tweetnacl before running the build command
  2. Include these in your project following these steps
  3. Include the compiled standard c++ library libc++_shared.so files located inside the Android NDK as in step 2 (reference)
    • You can find these inside the Android NDK for example under this path ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\lib

Usage

Create context

final ZContext context = ZContext();

Create socket

final ZSocket socket = context.createSocket(SocketType.req);

Connect socket

socket.connect("tcp://localhost:5566");

Send message

socket.send([1, 2, 3, 4, 5]);

Receive ZMessages

_socket.messages.listen((message) {
    // Do something with message
});

Receive ZFrames

_socket.frames.listen((frame) {
    // Do something with frame
});

Receive payloads (Uint8List)

_socket.payloads.listen((payload) {
    // Do something with payload
});

Destroy socket

socket.close();

Destroy context

context.stop();
Comments
  • Brain-free installation

    Brain-free installation

    I know it's kind of a huge task, but would it be theoretically possible to use ZeroMQ in Flutter without thinking about Android/Windows/iOS/Linux?

    What steps would be necessary?

    Would it be possible to distribute precompiled zeromq-Versions (.dll, .so etc) within this plugin?

    Is it theoretically possible to transpile the ZeroMQ Code to Dart only?

    We think this is an amazing plugin but we're really uncertain about the fact that you need to make sure that e.g. a precompiled .dll (windows) works on every machine (it does not).

    Priority: High Status: Completed Type: Enhancement 
    opened by Eerey 25
  • Fails to load .so libary on linux desktop

    Fails to load .so libary on linux desktop

    Launching lib/main.dart on Linux in debug mode...
    Building Linux application...
    Debug service listening on ws://127.0.0.1:37089/w-K6VIMgRmg=/ws
    Syncing files to device Linux...
    [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Exception: Could not load any zeromq library
    #0      ZContext._initBindings (package:dartzmq/src/zeromq.dart:77:7)
    #1      new ZContext (package:dartzmq/src/zeromq.dart:66:5)
    #2      main (package:remote_volume_control/main.dart:6:28)
    #3      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:130:25)
    #4      _rootRun (dart:async/zone.dart:1426:13)
    #5      _CustomZone.run (dart:async/zone.dart:1328:19)
    #6      _runZoned (dart:async/zone.dart:1861:10)
    #7      runZonedGuarded (dart:async/zone.dart:1849:12)
    #8      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:126:5)
    #9      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
    #10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
    
    [dartzmq] Failed to load library zmq:  Invalid argument(s): Failed to lookup symbol 'zmq_poller_new': /lib64/libzmq.so: undefined symbol: zmq_poller_new
    [dartzmq] Failed to load library libzmq:  Invalid argument(s): Failed to load dynamic library 'liblibzmq.so': liblibzmq.so: cannot open shared object file: No such file or directory
    [dartzmq] Failed to load library libzmq-v142-mt-4_3_5:  Invalid argument(s): Failed to load dynamic library 'liblibzmq-v142-mt-4_3_5.so': liblibzmq-v142-mt-4_3_5.so: cannot open shared object file: No such file or directory
    
    --------------------------------------------------
    
    sean@sean-fedora /lib64> ls | grep libzmq
    libzmq.so
    libzmq.so.5
    libzmq.so.5.2.4
    

    not entirely sure whats going on here the code is looking in the right place. any help would be appreciated

    Priority: Low Status: Completed Type: Question 
    opened by LordCommanderMay 10
  • send non-binary data

    send non-binary data

    Hi, why socket.send() parameter is from List<int> type? does it get data as binary array? what if someone wants to send data non-binary?

    and I have another question; how we can receive response from a req socket connection?

    Priority: Medium Status: Completed Type: Question 
    opened by NaarGes 7
  • Question: How to receive string message with subscription?

    Question: How to receive string message with subscription?

    Question At the moment I try to use your library for a pub/sub setup. I try to get string messages of my python publisher. If I try to write a subscriber with your dart library I have problems to get out the message as string. I tried your provided example too. But there I have the same problem... I do not get the string message, instead of a string I get some ZFrame... Can you help me with my problem? (Look at "To Reproduce") Thank you!

    Setup with python publisher and subscriber: Publisher:

    import zmq
    import random
    import sys
    import time
    
    port = "6555"
    if len(sys.argv) > 1:
        port = sys.argv[1]
        int(port)
    
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.bind("tcp://*:%s" % port)
    
    while True:
        # Receives a string format message
        socket.send_string("ML hello")
        time.sleep(1)
    

    Subscriber:

    # simple_sub.py
    import zmq
    
    host = "127.0.0.1"
    port = "6555"
    
    # Creates a socket instance
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    
    # Connects to a bound socket
    socket.connect("tcp://{}:{}".format(host, port))
    
    # Subscribes to all topics
    socket.subscribe("ML")
    
    while True:
        # Receives a string format message
        whole_message = socket.recv_string()
        topic, actual_message = whole_message.split(" ")
        print(actual_message)
    

    Environment OS: Archlinux 6.0.12-arch1-1 Android x86 emulator: Pixel 5 API 30 flutter: 3.3.9 compileSdkVersion 33 ndkVersion flutter.ndkVersion

    To Reproduce Subscriber with dart:

        // Tst
        final ZContext context = ZContext();
        final ZSocket socket = context.createSocket(SocketType.sub);
        late StreamSubscription _subscription;
        socket.connect("tcp://192.168.178.26:6555");
        socket.subscribe("ML");
    
        _subscription = socket.messages.listen((message) {
          print(message.toString());
        });
    

    Output:

    I/flutter (31175): ZMessage[ZFrame[[77, 76, 32, 104, 101, 108, 108, 111]]]

    Priority: Low Status: In Progress Type: Question 
    opened by alexanderwwagner 4
  • Incomplete data received by sub

    Incomplete data received by sub

    hi, did you find a problem? When subscribing to a topic under TCP protocol, big data will accept incompleteness, and I failed to set relevant properties.

    opened by gaobaiq 4
  • Connect/Send no error message if server is not available

    Connect/Send no error message if server is not available

    Describe the Bug I try to use your library with the environment which I described below. If I call socket.connect and Socket.send if no Server is available there is no error message and no effect... so I can not detect, that something is not okay?

    Environment OS: Archlinux 6.0.12-arch1-1 Android x86 emulator: Pixel 5 API 30 flutter: 3.3.9 compileSdkVersion 33 ndkVersion flutter.ndkVersion

    To Reproduce

      // Send a test message
      // => via zmq
      testzmq() {
        // Tst
        final ZContext context = ZContext();
        final ZSocket socket = context.createSocket(SocketType.req);
        socket.connect("tcp://localhost:6555");
        //socket.sendString("Hello");
        socket.send([1, 2]);
        socket.close();
      }
    
    

    Expected Behavior Error message if I can not connect to server / Get Stuck on server.connect

    Reproducibility 100%

    Priority: Low Status: Completed Type: Question 
    opened by alexanderwwagner 3
  • Failed to load dynamic library issue on MacOS platform

    Failed to load dynamic library issue on MacOS platform

    Hello,

    I have been trying to use dartzmq on flutter to establish a connection to a remote server host, but I keep getting the error:

    [dartzmq] Failed to load library zmq: Invalid argument(s): Failed to load dynamic library 'libzmq.so': dlopen failed: library "libzmq.so" not found [dartzmq] Failed to load library libzmq: Invalid argument(s): Failed to load dynamic library 'liblibzmq.so': dlopen failed: library "liblibzmq.so" not found [dartzmq] Failed to load library libzmq-v142-mt-4_3_5: Invalid argument(s): Failed to load dynamic library 'liblibzmq-v142-mt-4_3_5.so': dlopen failed: library "liblibzmq-v142-mt-4_3_5.so" not found

    I have built the MacOS platform in my local dartzmq repo and linked it to my flutter app. I was wondering if anyone could help me debug this library issue? If someone has dealt with this problem before, a simple list of steps to fix it would be much appreciated. Thanks!

    opened by richsi 2
  • when running example code to receive message via listen funcion, dartzmq throws exception

    when running example code to receive message via listen funcion, dartzmq throws exception

    I don't know if the reason to this issue happens is the project is now still under development stage. but through observing the source code, I found that maybe one of the reason is the dartzmq doesn't provide the receive function, and the inner strategy is using loop to handle the receive data without sending data for reply. but whether I use reply mode or request mode, zmq doesn't allow no sending action within two receiving actions. so, it seemed the exception occurs. i want to know why the dartzmq has not provide receive funciton directly? or the dartzmq developer has had a solution to solove the problem? I am looking forward to the reply very eagerly. Thanks in advance.

    Priority: Low Status: Abandoned Type: Bug 
    opened by bigchen2008 1
  • Added linux instructions to README and updated example to include echo

    Added linux instructions to README and updated example to include echo

    I updated the example project to include an echo socket to have a more complete example. I also switched the sockets to a dealer/router pair as I was getting an occasional exception with the req/rep pair. Those can be fussy as they change states often, so that might be something to look into further.

    opened by ach-ee 1
  • ZeroMQ seems to

    ZeroMQ seems to "wait" one second before receiving data.

    Hello there,

    I'm testing this feature on windows. When using a push/pull or pub/sub pattern there seems to be a one second delay on the pull/sub socket. Is it built into ZeroMQ or can i configure this behaviour?

    Best regards

    Priority: Medium Status: In Progress Type: Enhancement 
    opened by Eerey 10
Releases(1.0.0-dev.7)
  • 1.0.0-dev.7(Oct 29, 2021)

    Minor documentation improvements

    • Fix rename of SocketMode to SocketType in README
    • Add receiving messages (ZMessage, ZFrame and payloads) to README and example
    • Override toString function in ZMessage and ZFrame for better debugging experience
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0-dev.6(Oct 25, 2021)

    Free pointers before throwing a ZeroMQException

    • Free pointers before throwing a ZeroMQException
    • Add return code to zmq_setsockopt function
    • Add return code check to ZSocket.setOption function
    • Add zmq_has function for checking supported capabilities
    • Add helper functions for zmq_has
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0-dev.5(Oct 21, 2021)

    Fix destroying poller and loading shared library

    • Rename SocketMode to SocketType
    • Add some steps on how to use dartzmq on Android
    • Address warnings in bindings.dart
    • Fix destroying poller (use **poller instead of *poller)
    • Add more class documentation
    • Fix loading shared library for orher platforms
    • Extend error messages
    • Add more socket options
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0-dev.4(Oct 11, 2021)

    Fix heap corruption due to wrong usage of malloc.allocate

    • Use periodic timer to poll sockets every second
    • Poll all messages on socket instead of one for each event to not loose messages
    • Reuse zeromq message pointer
    • Improve return code handling
    • Rename _isActive of ZContext to _shutdown
    • Rename _handle and _zmq of ZSocket to _socket and _context
    • Add stream for ZFrames to ZSocket
    • Always show error code in ZeroMQException
    • Fix pubspec of example
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0-dev.3(Oct 10, 2021)

    Add example, subscriptions for sub sockets and code cleanup

    • Add minimal working example
    • Rename ZmqSocket to ZSocket
    • Rename ZeroMQ to ZContext
    • Rename ZeroMQBindings tor ZMQBindings
    • Add subscribe(String topic) and unsubscribe(String topic) to manage subscriptions of sub sockets
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0-dev.2(Oct 10, 2021)

    Add support for multipart messages

    • Rename Message to ZFrame
    • Add ZMessage as a queue of ZFrame's
    • Receive messages as ZMessage instead of Message(ZFrame)
    • Reduce minimum SDK version to 2.13.0
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0-dev.1(Oct 10, 2021)

    Add crude implementation of libzmq

    • Creating sockets (pair, pub, sub, req, rep, dealer, router, pull, push, xPub, xSub, stream)
    • Sending messages (of type List<int>)
    • Bind (bind(String address))
    • Connect (connect(String address))
    • Curve (setCurvePublicKey(String key), setCurveSecretKey(String key) and setCurveServerKey(String key))
    • Socket options (setOption(int option, String value))
    Source code(tar.gz)
    Source code(zip)
Owner
Moritz Wirger
Full stack developer
Moritz Wirger
App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and less verbose.

App HTTP Client App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and le

Joanna May 44 Nov 1, 2022
Easy nav - A simple wrapper around flutter navigator, dialogs and snackbar to do those things without context

EasyNav Just a simple wrapper around flutter navigator, dialogs and snackbar to

Abdul Shakoor 2 Feb 26, 2022
A wrapper around Navigator 2.0 and Router/Pages to make their use a easier.

APS Navigator - App Pagination System This library is just a wrapper around Navigator 2.0 and Router/Pages API that tries to make their use easier: ??

Guilherme Silva 14 Oct 17, 2022
Shared preferences typed - A type-safe wrapper around shared preferences, inspired by ts-localstorage

Typed Shared Preferences A type-safe wrapper around shared_preferences, inspired

Philipp Bauer 0 Jan 31, 2022
A most easily usable Duolingo API wrapper in Dart. Duolingo4D is an open-sourced Dart library.

A most easily usable Duolingo API wrapper in Dart! 1. About Duolingo4D Duolingo4D is an open-sourced Dart library. With Duolingo4D, you can easily int

Kato Shinya 18 Oct 17, 2022
Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur

❤️ Sponsor get_it This is a simple Service Locator for Dart and Flutter projects with some additional goodies highly inspired by Splat. It can be used

Flutter Community 1k Jan 1, 2023
A most easily usable RESAS API wrapper in Dart. With this library, you can easily integrate your application with the RESAS API.

A most easily usable RESAS API wrapper library in Dart! 1. About 1.1. What Is RESAS? 1.2. Introduction 1.2.1. Install Library 1.2.2. Import It 1.2.3.

Kato Shinya 2 Apr 7, 2022
A most easily usable JSON wrapper library in Dart

A most easily usable JSON response wrapper library in Dart! 1. About 1.1. Introd

Kato Shinya 2 Jan 4, 2022
The lightweight and powerful wrapper library for Twitter Ads API written in Dart and Flutter 🐦

TODO: Put a short description of the package here that helps potential users know whether this package might be useful for them. Features TODO: List w

Twitter.dart 2 Aug 26, 2022
Binding and high-level wrapper on top of libssh - The SSH library!

Dart Binding to libssh version 0.9.6 binding and high-level wrapper on top of libssh - The SSH library! libssh is a multiplatform C library implementi

Isaque Neves 2 Dec 20, 2021
DiceBear API wrapper. DiceBear is an avatar library for designers and developers. Generate random avatar profile pictures!

dice_bear Flutter Package DiceBear API wrapper. DiceBear is an avatar library for designers and developers. Generate random avatar profile pictures! C

Zaif Senpai 8 Oct 31, 2022
Dart wrapper via `dart:js` for webusb

Dart wrapper via dart:js for https://wicg.github.io/webusb/ Features canUseUsb g

Woodemi Co., Ltd 1 Jan 25, 2022
A simple typewriter text animation wrapper for flutter.

A simple typewriter text animation wrapper for flutter, supports iOS, Android, web, Windows, macOS, and Linux.

Nialixus 1 Jun 1, 2022
Play Around with Dartpad and fall in love with Flutter

Flutter Workshop - GDSC NIT ROURKELA - Preptember 2021 What we built during the workshop? For checking out the code For checking out the code For chec

Astha Nayak 4 Feb 27, 2022
Breathe is a mental health blogging app where users can join communities of doctors and other users from around the world and both share their problems as well as lend a ear to and help others

?????????????? ?????????????? In a condensed, suffocating society you can feel closed off, when you can't process your emotions and are going through

Soham Sen 3 May 16, 2022
A flutter package which will help you to create a draggable widget that can be dragged around the screen.

A flutter package which will help you to create a draggable widget that can be dragged around the screen. Demo Features ?? Manually Control the positi

Adar 31 Aug 10, 2022
Mobile App for posting and reporting incidents around your area.

newsecurus A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if th

John Philip Dela Vega 1 Dec 8, 2021
An application to track bitcoin prices around the world.

![App Brewery Banner](https://github.com/londonappbrewery/Images/blob/master/AppBreweryBanner.png) # Bitcoin Ticker ?? ## Our Goal The object

Aryaman Prakash 1 Jan 7, 2022
Behruz Hurramov 0 Dec 29, 2021