Making COAP request from my Custom Node

#1

Hi,

I am creating a custom node which should be able to make a COAP request while deploying. So I need to implement that COAP request inside the javascript file of my custom node. I use "coap" npm module for this.

    module.exports = function(RED) {

    function myCustomNode(config) {
    RED.nodes.createNode(this, config);

     var coap = require('coap')
     var req = coap.request('coap://localhost/hello')

    req.on('response', function (res) {
        res.pipe(process.stdout)
        res.on('end', function () {
            process.exit(0)
        })
    })

    req.end();

    RED.nodes.registerType("myCustomNode", myCustomNode);
    }

Hope my understanding is correct up to this level. Please correct me if I am wrong.

My code looks like above. I checked the COAP call source code part in node.js application and it is working fine. But I am not getting the response when I deploy my custom node.

What is the reason for this?

0 Likes

#2

What exactly are you expecting that code to do? Where do you expect the response to appear?

From what I can see, not being familiar with the coap module, it will write the response to stdout (ie the node-red log) and then exit the node.js process - killing node-red.

0 Likes

#3

Okay now I am getting it. Thank you very much for explaining what is going on there. The problem was killing the process and I am getting the output when I remove it.

What I want to do is get the response into a variable for other tasks, not just print it to the log. How can I read the response string into a variable (ex: and then print it to the log)?

I printed it to my console using console.log(res) and got following. Not clear how to get the body inside payload

    IncomingMessage {
      _readableState: 
       ReadableState {
         objectMode: false,
         highWaterMark: 16384,
         buffer: [],
         length: 0,
         pipes: null,
         pipesCount: 0,
         flowing: null,
         ended: false,
         endEmitted: false,
         reading: false,
         sync: true,
         needReadable: false,
         emittedReadable: false,
         readableListening: false,
         resumeScheduled: false,
         defaultEncoding: 'utf8',
         ranOut: false,
         awaitDrain: 0,
         readingMore: false,
         decoder: null,
         encoding: null },
      readable: true,
      domain: null,
      _events: {},
      _eventsCount: 0,
      _maxListeners: undefined,
      payload: <Buffer 48 65 6c 6c 6f 20 74 68 65 72 65>,
      options: [],
      code: '2.05',
      method: undefined,
      headers: {},
      url: '/',
      rsinfo: { address: '127.0.0.1', family: 'IPv4', port: 5683, size: 20 },
      outSocket: { address: '0.0.0.0', family: 'IPv4', port: 41185 },
      _packet: 
       { code: '2.05',
         confirmable: false,
         reset: false,
         ack: true,
         messageId: 24618,
         token: <Buffer d8 41 e0 57>,
         options: [],
         payload: <Buffer 48 65 6c 6c 6f 20 74 68 65 72 65> },
      _payloadIndex: 0 }
    ```
0 Likes

#4

from that it looks like it should be something like
var pay = res.IncomingMessage.payload;
(just console.log things like that until you find what you want)
It is a Buffer which is binary - but if you know it will always be a string (no binary characters) you can probably just
pay = pay.toString();

1 Like