https://github.com/megahertz/pascal-nodejs-ipc
Free Pascal library for communicating with a parent Node.js process through IPC
https://github.com/megahertz/pascal-nodejs-ipc
Last synced: about 1 year ago
JSON representation
Free Pascal library for communicating with a parent Node.js process through IPC
- Host: GitHub
- URL: https://github.com/megahertz/pascal-nodejs-ipc
- Owner: megahertz
- License: mit
- Created: 2019-04-18T16:27:10.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-26T20:02:02.000Z (about 7 years ago)
- Last Synced: 2025-04-03T15:17:56.535Z (about 1 year ago)
- Language: Pascal
- Size: 491 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pascal-nodejs-ipc
Free Pascal library for communicating with a parent Node.js process through
Inter Process Communications (IPC).
Tested with FPC v3.0.4, v3.1.1, node v11.9.0 on Linux x64
[Example project](example)
## Usage
**parent.js**
```js
const { spawn } = require('child_process');
const proc = spawn('./dist/child', {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
});
// Using built-in Node.js IPC
proc.on('message', msg => console.log('parent, new message:', msg));
proc.send({ source: 'Node.js' });
```
**child.pas**
```pascal
program child;
uses
{$ifdef Unix}cthreads,{$endif} nodeipc;
type
TApplication = class
private
FNodeIpc: TNodeIpcProtocol;
procedure OnMessage(Message: Variant);
public
procedure Run;
end;
procedure TApplication.OnMessage(Message: Variant);
begin
WriteLn('child, new message: ', Message);
end;
procedure TApplication.Run;
begin
FNodeIpc := TNodeIpcProtocol.Create;
FNodeIpc.OnMessage := @OnMessage;
FNodeIpc.Send('{"source":"Free Pascal"}');
WriteLn('Press any key to exit');
ReadLn();
end;
var
App: TApplication;
begin
App := TApplication.Create;
App.Run;
end.
```
## How Node.js built-in IPC module works
- Node.js passes a bi-directional IPC stream to a child process as a file
descriptor.
```js
const proc = spawn('./dist/child', {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
});
```
- To send a message trough this stream, each process can send JSON encoded
strings separated by a new line char.
- To get the exact file descriptor, a child process reads NODE_CHANNEL_FD
environment variable.