jensstigaard / node-vmix

Node.js vMix utility to easily send commands and receive info such as xml data or real time tally from a vMix instance
MIT License
25 stars 7 forks source link

Is it possible to poll for the inputs #18

Closed chandrashekar-nallamilli closed 1 year ago

chandrashekar-nallamilli commented 1 year ago

I am trying to get the input state every second , How ever i am not successful in doing so as the socket connection is getting closed. Is there a better way to do this ? setInterval(async () => { const vMixConn = new ConnectionTCP('localhost', { debug: true, autoReconnect: true, }); vMixConn.on('connect', (err: any) => { if (err) { console.error(err); return; } console.log('Connected!'); vMixConn.send('XML'); }); vMixConn.on('xml', async (data) => { const xmlDocument = vMixXmlApi.DataParser.parse(data); const xmlInputs = vMixXmlApi.Inputs.extractInputsFromXML(xmlDocument); const inputs = sanitize(vMixXmlApi.Inputs.map(xmlInputs)); console.log(inputs) }); }, 1000);

This is the log [node-vmix] Socket connection closed [node-vmix] Initialising reconnecting procedure...

jensstigaard commented 1 year ago

Hi @chandrashekar-nallamilli

As I see it, you create a connection to the vMix instance every second.. This is not desired! Since the library runs a TCP tunnel to the vMix instance, the best practice is to use just one connection.

So instead of your code:

setInterval(async () => { 
const vMixConn = new ConnectionTCP('localhost', { debug: true, autoReconnect: true, }); 
vMixConn.on('connect', (err: any) => { if (err) { console.error(err); return; } console.log('Connected!'); vMixConn.send('XML'); }); vMixConn.on('xml', async (data) => { const xmlDocument = vMixXmlApi.DataParser.parse(data); const xmlInputs = vMixXmlApi.Inputs.extractInputsFromXML(xmlDocument); const inputs = sanitize(vMixXmlApi.Inputs.map(xmlInputs)); console.log(inputs) }); }, 1000);

I would move all the logic out of the setInterval, and inside the on('connect') have the setInterval, which calls vMixConn.send('XML'):

const vMixConn = new ConnectionTCP('localhost', { debug: true, autoReconnect: true, }); 

vMixConn.on('connect', (err: any) => {
   if (err) { 
      console.error(err); 
      return; 
   } 

   console.log('Connected!'); 
   setInterval(async () => { 
      vMixConn.send('XML'); 
   }, 1000);
 });

vMixConn.on('xml', async (data) => { 
   const xmlDocument = vMixXmlApi.DataParser.parse(data); 
   const xmlInputs = vMixXmlApi.Inputs.extractInputsFromXML(xmlDocument); 
   const inputs = sanitize(vMixXmlApi.Inputs.map(xmlInputs)); 
   console.log(inputs) 
});
chandrashekar-nallamilli commented 1 year ago

Thank you it worked 👍🏽

jensstigaard commented 1 year ago

@chandrashekar-nallamilli ?? You reopened the issue. Did you experience any problems??

chandrashekar-nallamilli commented 1 year ago

Hi @jensstigaard is there way to close the connection . Currently i am using ClearAllListners and Shutdown functions. is there a better way to clear ?. I tried off function but never got it going The usecase is when user logged out of the server we should clear all the active connections

jensstigaard commented 1 year ago

What do you mean by "all the active connections"? You should really just have one TCP connection. The function .shutdown() closes the connection.

chandrashekar-nallamilli commented 1 year ago

Hi Yes, I moved the logic to have one connection for the entire application. Closing the issue. thanks for the help