test | ||
ts | ||
.gitignore | ||
.gitlab-ci.yml | ||
license | ||
npmextra.json | ||
package-lock.json | ||
package.json | ||
readme.hints.md | ||
readme.md | ||
tsconfig.json |
@push.rocks/smartipc
node inter process communication
Install
To install @push.rocks/smartipc, use the following command with npm:
npm install @push.rocks/smartipc --save
This command adds @push.rocks/smartipc
to your project's dependencies.
Usage
@push.rocks/smartipc
simplifies inter-process communication (IPC) in Node.js applications, wrapping the complexity of IPC setup into an easy-to-use API. It supports both server and client roles within IPC, allowing processes to communicate with each other efficiently. Below, you'll find comprehensive guides and examples to quickly incorporate smartipc
into your Node.js projects.
Getting Started
First, import SmartIpc
from @push.rocks/smartipc
in your TypeScript file:
import { SmartIpc } from '@push.rocks/smartipc';
Setting Up a Server
To set up an IPC server, create an instance of SmartIpc
with the type set to 'server'
. Define a unique ipcSpace
name, which serves as the namespace for your IPC communication.
const serverIpc = new SmartIpc({
type: 'server',
ipcSpace: 'myUniqueIpcSpace'
});
Registering Handlers
Before starting your server, register message handlers. These handlers listen for specific keywords and execute corresponding functions when messages arrive.
serverIpc.registerHandler({
keyword: 'greeting',
handlerFunc: (dataArg: string) => {
console.log(`Received greeting: ${dataArg}`);
}
});
Starting the Server
(async () => {
await serverIpc.start();
console.log('IPC Server started!');
})();
Setting Up a Client
Setting up a client is similar to setting up a server. Create a SmartIpc
instance with the type set to 'client'
and use the same ipcSpace
name used for the server.
const clientIpc = new SmartIpc({
type: 'client',
ipcSpace: 'myUniqueIpcSpace'
});
Starting the Client
(async () => {
await clientIpc.start();
console.log('IPC Client connected!');
})();
Sending Messages
Once the client and server are set up and running, you can send messages using sendMessage
. Specify the message identifier and the message content. The server will receive this message and process it using the registered handler.
// From the client
clientIpc.sendMessage('greeting', 'Hello from the client!');
Clean Up
It's a good practice to gracefully stop the IPC server and client when they're no longer needed.
// Stopping the client
(async () => {
await clientIpc.stop();
console.log('IPC Client disconnected!');
})();
// Stopping the server
(async () => {
await serverIpc.stop();
console.log('IPC Server stopped!');
})();
Advanced Usage
Handling JSON Messages
While @push.rocks/smartipc
allows sending strings directly, you might often need to send structured data. The sendMessage
method can handle objects by converting them to JSON strings before sending.
// Sending an object from the client
clientIpc.sendMessage('data', { key: 'value' });
// Server handler for 'data'
serverIpc.registerHandler({
keyword: 'data',
handlerFunc: (dataArg: string) => {
const dataObject = JSON.parse(dataArg);
console.log(dataObject.key); // Outputs: value
}
});
Error Handling
Always include error handling in production applications to manage unexpected scenarios, such as disconnection or message parsing errors.
// Example of adding error handling to the server start process
(async () => {
try {
await serverIpc.start();
console.log('IPC Server started!');
} catch (error) {
console.error('Failed to start IPC Server:', error);
}
})();
Conclusion
Integrating @push.rocks/smartipc
into your Node.js applications streamlines the process of setting up IPC for inter-process communication. Through the examples provided, you've seen how to configure both servers and clients, register message handlers, send messages, and incorporate error handling. With smartipc
, you can facilitate robust communication channels between different parts of your application, enhancing modularity and process isolation.
For further information and advanced configuration options, refer to the @push.rocks/smartipc
documentation.
License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.
Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.