Giter Club home page Giter Club logo

Comments (10)

KamakshiOjha avatar KamakshiOjha commented on June 3, 2024 4

Neutralinojs allows communication with the parent process using standard input (stdin) and standard output (stdout). This can be useful for exchanging data and commands between Neutralinojs applications and their parent processes.

Receiving Data from Parent Process (stdin):

const { stdin } = require('@neutralinojs/neu');

stdin.on('data', (data) => {
console.log(Received data from parent process: ${data});
// Process the received data as needed
});

Sending Data to Parent Process (stdout):

const { stdout } = require('@neutralinojs/neu');

// Send data to the parent process
stdout.write('Data to send to the parent process\n');

Bidirectional Communication:

const { stdin, stdout } = require('@neutralinojs/neu');

// Listen for data from the parent process
stdin.on('data', (data) => {
console.log(Received data from parent process: ${data});

// Process the received data and send a response
const response = `Processed: ${data}`;
stdout.write(response);

});

from neutralinojs.

shalithasuranga avatar shalithasuranga commented on June 3, 2024 2

Hello, thanks for your explanation. Currently, there is no official API to read the main process's stdin, but we can add a new function like app.readStdIn(untilEof=true/false) (or in filesystem) and a new event processStdIn in the next release (Feel free to add your suggestions for the function). I'll add this issue as a feature request.

I think some alternative methods with the current release are:

  • Writing a custom API function by editing the source.
  • Using CLI arguments to pass a file input, read it using Neutralino APIs, write it to a file, and read it using cat.

Thanks for posting this feature request 🎉

from neutralinojs.

Hill-98 avatar Hill-98 commented on June 3, 2024 1

Cool! I'll give it a try.

from neutralinojs.

shalithasuranga avatar shalithasuranga commented on June 3, 2024

Hello @Hill-98, do you need to communicate with the Neutralinojs framework process? For that, you can use the framework's WebSocket connection. If you need to communicate with a child process, you can use the os.spawnProcess function.

from neutralinojs.

Hill-98 avatar Hill-98 commented on June 3, 2024

@shalithasuranga Thanks for your reply, but it's neither of those, I need to communicate with the parent process.

like this:
cat some.txt | neutralinojs-program | grep some

Can I handle stdin and stdout?

from neutralinojs.

LIZA1608 avatar LIZA1608 commented on June 3, 2024

good one

from neutralinojs.

Abhishekkar497 avatar Abhishekkar497 commented on June 3, 2024

To send data from Neutralino to the parent process, you can write to the stdout stream. Here's an example in JavaScript:
// Send data to the parent process
function sendDataToParent(data) {
console.log(JSON.stringify(data));
}
In this example, JSON.stringify(data) converts the data into a JSON string, and console.log writes it to the stdout stream. You can call this function whenever you want to send data to the parent process.

To receive data in Neutralino from the parent process, you can read from the stdin stream. Here's an example:
// Read data from stdin
process.stdin.on('data', (data) => {
const receivedData = JSON.parse(data);
handleReceivedData(receivedData);
});

// Handle the received data
function handleReceivedData(data) {
// Process the received data
console.log('Received data from parent process:', data);
}
In this example, the process.stdin.on event listener is used to listen for data on the stdin stream. When data is received, it is parsed using JSON.parse to convert it from a JSON string to a JavaScript object. You can then handle the received data in the handleReceivedData function.
Hope this helps.

from neutralinojs.

shalithasuranga avatar shalithasuranga commented on June 3, 2024

Hello everyone, now we have added readProcessInput(readAll=true/false), writeProcessOutput(), and writeProcessError() functions. We'll release them with the next framework version soon. Thanks for reporting and discussing this 🎉

Example (testable by tomorrow with nightly):

await Neutralino.app.writeProcessOutput('Enter your name: '); 
let name = await Neutralino.app.readProcessInput();
await Neutralino.app.writeProcessOutput(`Hello ${name}...`); 

from neutralinojs.

Hill-98 avatar Hill-98 commented on June 3, 2024

@shalithasuranga Are these implemented correctly only on Linux?

I found that Windows didn't work. It seems to be because the Windows version works in "Window mode" instead of "Console mode". As far as I understand, one Windows program cannot implement both modes at the same time, so is it possible to provide another binary for Windows separately? (Like: neutralino-win_x64_console.exe)

from neutralinojs.

shalithasuranga avatar shalithasuranga commented on June 3, 2024

Hey @Hill-98, thanks for posting this info. Mm. yeah, according to internet documentations, I also found the same thing you've mentioned about Windows executables. The issue is that I couldn't test this feature on Windows since our Azure sponsorship was terminated. Looks like we need to get some help from a contributor until I re-apply for the Azure sponsorship. Thanks 🎉

from neutralinojs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.