Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
Error with zmq and express

I'm trying to create an API that sends an information obtained by a socket.
To get the information from my socket, I'm using zmq.
The idea is when got the information from the socket, send this info as a response in my API. The following code pretends do that.

[...]
enclaveSocket.on('message', function (message) {

    res_json.info = message.toString('base64');

    res.json(res_json);
});
enclaveSocket.send('chesf:public');
[...]

In the first request to my express API, the return is the expected:

{
  "organization": "my_org",
  "info": "AjCWyQeBNeuMnA3nAUrbprxctrdOgZmjDR5Rmuw6LzzMpALspfNapLMoRGPx9YEGFrqrE5vdHKPb/z0S23i8v"
}

But in the second request my API crashes with the following error:

events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:367:11)
at ServerResponse.header (/home/matteus/keyvault-node/node_modules/express/lib/response.js:725:10)
at ServerResponse.send (/home/matteus/keyvault-node/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/matteus/keyvault-node/node_modules/express/lib/response.js:256:15)
at exports.Socket.<anonymous> (/home/matteus/keyvault-node/routes/keys.js:58:21)
at emitOne (events.js:101:20)
at exports.Socket.emit (events.js:188:7)
at exports.Socket.Socket._flushRead (/home/matteus/keyvault-node/node_modules/zmq/lib/index.js:636:10)
at exports.Socket.Socket._flushReads (/home/matteus/keyvault-node/node_modules/zmq/lib/index.js:676:23)
at Object._zmq.onReadReady (/home/matteus/keyvault-node/node_modules/zmq/lib/index.js:297:10)

That's it.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I solved my problem using enclaveSocket.once() instead of enclaveSocket.on().
The once() method removes event listeners when the event is handled by the same function.

More information about here: http://nodeguide.com/beginner.html#using-eventemitters

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!