0

I have imported a module to another module and called it in that module and its working nicely but i want to store data which is in my imported module how to i do that? please help

This is the code of the module which got imported


async function dfuseListener()
{
  const client = createDfuseClient({
    apiKey: 'mobile_47115651c24f79d220184dae661259df',
    network: "jungle",

    httpClientOptions: {
        fetch: nodeFetch,

      },

      streamClientOptions: {
        socketOptions: {
          webSocketFactory: async (url) => {
              console.log("hiiiiiii")
            const webSocket = new WebSocket(url, {
              handshakeTimeout: 30 * 1000, // 30s
              maxPayload: 200 * 1024 * 1000 * 1000 // 200Mb

                        })
            const onUpgrade = (response) => {
              console.log("Socket upgrade response status code.", response.statusCode)

              // You need to remove the listener at some point since this factory
              // is called at each reconnection with the remote endpoint!
              webSocket.removeListener("upgrade", onUpgrade)
            }

            webSocket.on("upgradeurl", onUpgrade)

            return webSocket
          }
        }
      }

  })

  const onMessage = (message) => {

    console.log('onMessage :')
    console.log('im working ',message);
   if (message.type === InboundMessageType.PROGRESS) {
      // updateProgress(networkName, message);
    } else if (message.type === InboundMessageType.ACTION_TRACE) {
    //  updateAction(message.data);
    } else {
       console.log(message);
    }
  }
  const stream = await client.streamActionTraces(
    {
      accounts: "guru11111111"
    },
    onMessage, {
      start_block:  52797826,
      with_progress: 60
    }
  )  
  console.log("Socket is now connected.")

}
module.exports=dfuseListener;

I'm calling dfuseListener in my other module and it printing the value in console

console.log('im working ',message); i want to access the value of message in my other module and store it in mongodb

Is there any way i can access that ?

this is the 2nd module in which i want to access the imported module data

const MongoClient = require('mongodb').MongoClient;
const express =require('express')
const app =express();

const dfuseListener = require('./dfuse');
dfuseListener();
console.log('dfuse product: ',dfuseListener.onMessage)//Its giving undefine

app.get('/',(req,res)=> {
    res.send("hey its working ..")

})
app.listen(9000);

const uri = "mongodb+srv://abc:123@cluster0-z2ii6.mongodb.net/test?retryWrites=true&w=majority"
MongoClient({ useUnifiedTopology: true });  
MongoClient.connect(uri,{ useNewUrlParser: true },{ useUnifiedTopology: true },function(err, client) {
   if(err) {
        console.log('Error occurred while connecting to MongoDB Atlas...\n',err);
   }
   console.log('Connected...');
   const collection = client.db("test").collection("devices");
   // perform actions on the collection object
   client.close();
});



guruprakash gupta
  • 877
  • 4
  • 9
  • 15
  • try this `dfuseListener().then(res => console.log(res));` what will you get? – Daniyal Lukmanov Oct 03 '19 at 08:42
  • 1
    The `dfuseListener` method is not returning anything, so you won't have a `.onMessage` available. – painotpi Oct 03 '19 at 08:42
  • That is correct, because as @ta-run said there is nothing returned in that function. – Daniyal Lukmanov Oct 03 '19 at 08:55
  • 1
    Basically you are approaching this the wrong way around. Instead of this function returning anything to the other module ( as it were ) instead the "other module" should be injecting a "handler" into the function instead. Or alternately your function can set up the "stream" and then you register the `.on()` event handlers in the block of code ( i.e within the "other module" ) in which you want to see results returned. In short you are missing some understanding of concepts with asynchronous methods, for which reading the existing linked responses should be of help to you. – Neil Lunn Oct 03 '19 at 08:59
  • To make it work, you can return `onMessage` from inside your `dfuseListener` method and create your `dfuseListener`object inside a `then`. https://jsfiddle.net/41prz258/ In general though, the async-await implementation seems incorrect, would advise you read up on that and clean it up :) – painotpi Oct 03 '19 at 09:03
  • @ta-run Its returning the whole structure of the fuction onMessage but i want the data in argument message – guruprakash gupta Oct 03 '19 at 09:26

0 Answers0