Is ZeroMQ for Node.js compatible with Electron?
Asked Answered
D

2

5

I am have a huge headache from trying to get the ZMQ Node bindings working with Electron, especially on Windows. I am working on Windows 7 and Ubuntu 16.04 and both of them have two separate issues.

On Windows, I get an error when I try to do require('zmq')

C:\vueelectron\app\node_modules\bindings\bindings.js:91 Uncaught Error: Could not locate the bindings file. Tried:
 → C:\vueelectron\app\node_modules\zmq\build\zmq.node
 → C:\vueelectron\app\node_modules\zmq\build\Debug\zmq.node
 → C:\vueelectron\app\node_modules\zmq\build\Release\zmq.node
 → C:\vueelectron\app\node_modules\zmq\out\Debug\zmq.node
 → C:\vueelectron\app\node_modules\zmq\Debug\zmq.node
 → C:\vueelectron\app\node_modules\zmq\out\Release\zmq.node
 → C:\vueelectron\app\node_modules\zmq\Release\zmq.node
 → C:\vueelectron\app\node_modules\zmq\build\default\zmq.node
 → C:\vueelectron\app\node_modules\zmq\compiled\6.1.0\win32\x64\zmq.node

I've tried compiling with VS 2013 and 2015, rebuilt multiple times, used electron-rebuild nothing seems to be working.

On Linux it loads up fine but the problem is that when I send a message, it seems to get stuck in a loop somewhere and it keeps sending sending hundreds of messages and goes on doing that indefinitely. This was resolved by upgrading from the version of ZMQ in the Ubuntu repositories to the latest one downloaded from the ZeroMQ website.

This is the code I used in my index.html file of my Electron app.

const electron = require('electron')
const zmq = require('zmq')

const socket = zmq.socket('req')
socket.connect('tcp://10.10.0.51:3111')

socket.on('message', function (data) {
  console.log(socket.identity + ': answer data ' + data)
})

socket.send('test')

Has anyone else been able to get Electron + ZMQ working? If so, what is your development enviroment like? Thanks.

Dawson answered 18/7, 2016 at 14:53 Comment(1)
A detailed instruction has been added in my answer, please check.Pompano
P
7

The problem is the unmatched node.js binary that is delivered by Electron and your version of node. The long answer is that you need to compile Electron and ZeroMQ with the same Node.js headers. Here is the response from Electron community http://github.com/electron/electron/issues/6805. There's a short answer now though!

Use zeromq in place of zmq (same API). zeromq provides prebuilt binaries for electron and node.js for OS X, Windows, and macOS/OS X. After installing zeromq, rebuild for the version of electron you're using:

npm rebuild zeromq --runtime=electron --target=1.4.5

Thanks to the zeromq.js team and have fun with ZeroMQ!

Pompano answered 11/8, 2016 at 4:15 Comment(2)
Thanks for the reply, the link is down though. I was able to resolve my problem by using electron-builder, I am not sure what it does differently but it works now without fail.Dawson
We've done one more step to make it even easier for you -- zmq-prebuilt (renamed zeromq.js, zeromq on npm) now ships prebuilt binaries for electron releases: github.com/zeromq/zeromq.js#rebuilding-for-electronStrephon
R
0

It might be safer to put access to your queue behind an api layer. You might have better success with stability too, native modules in electron can be very tricky.

And but that I mean have a REST server which your electron application communicates with. It would send a message to that api, which then queues the message for your application. Restrict access to the queue at the network level to only the api server.

Romero answered 18/7, 2016 at 21:26 Comment(2)
Thanks for the response, I actually am coming from that scenario. But we are trying to keep the latency to a minimum and all the HTTP servers we tried added at least 30 ms or so. It also is an extra layer of complexity I'd like to avoid. The weird part is how easy it is to get it working on Linux and Mac.Dawson
I appreciate this idea. But how to make the api layer transparent? E.g. I have a pure node.js application with a lot of zmq code. Now I have to add an api layer for Electron app. I don't want to change existing code much, but just replace all require("zmq") with require("zmq-bridge"). Shall I check every single mehthod and do the mapping one by one?Pompano

© 2022 - 2024 — McMap. All rights reserved.