You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Node Connector Communication ModuleThis module simplifies communication between Node.js and Phoenix (phcode). A `NodeConnector` acts as an intermediary,allowing you to execute functions in Node.js from Phoenix and vice versa. You can use the `execPeer` method to callfunctions on the other side and handle communication seamlessly. Use `triggerPeer` to trigger eventson the other side.## Setting Up a `NodeConnector`To establish communication between two modules, such as `x.js` in Phoenix and `y.js` in Node.js, follow these steps:### Create `NodeConnector` in Phoenix (`x.js`)
9
+
Node Connector Communication Module
10
+
11
+
This module simplifies communication between Node.js and Phoenix (phcode). A `NodeConnector` acts as an intermediary,
12
+
allowing you to execute functions in Node.js from Phoenix and vice versa. You can use the `execPeer` method to call
13
+
functions on the other side and handle communication seamlessly. Use `triggerPeer` to trigger events
14
+
on the other side.
15
+
16
+
## Setting Up a `NodeConnector`
17
+
18
+
To establish communication between two modules, such as `x.js` in Phoenix and `y.js` in Node.js, follow these steps:
19
+
20
+
### Create `NodeConnector` in Phoenix (`x.js`)
10
21
11
22
**Example**
12
-
```jsconst NodeConnector = require('NodeConnector');const XY_NODE_CONNECTOR_ID = 'ext_x_y'; // Use a unique IDlet nodeConnector = NodeConnector.createNodeConnector(XY_NODE_CONNECTOR_ID, exports);exports.modifyImage = async function(imageName, imageArrayBuffer) { // Perform image operations with the imageArrayBuffer // To return an ArrayBuffer, return an object with a `buffer` key. return { operationDone: 'colored, cropped', buffer: imageArrayBuffer, };};```### Create `NodeConnector` in Node.js (`y.js`)
23
+
```js
24
+
constNodeConnector=require('NodeConnector');
25
+
constXY_NODE_CONNECTOR_ID='ext_x_y'; // Use a unique ID
26
+
let nodeConnector =NodeConnector.createNodeConnector(XY_NODE_CONNECTOR_ID, exports);
// Perform image operations with the imageArrayBuffer
30
+
// To return an ArrayBuffer, return an object with a `buffer` key.
31
+
return {
32
+
operationDone:'colored, cropped',
33
+
buffer: imageArrayBuffer,
34
+
};
35
+
};
36
+
```
37
+
38
+
### Create `NodeConnector` in Node.js (`y.js`)
13
39
**Example**
14
-
```jsconst XY_NODE_CONNECTOR_ID = 'ext_x_y'; // Use the same unique IDlet nodeConnector = global.createNodeConnector(XY_NODE_CONNECTOR_ID, exports);exports.getPWDRelative = async function(subPath) { return process.cwd + '/' + subPath;};```With these steps, a `NodeConnector` is set up, enabling two-way communication.## Executing FunctionsTo call a Node.js function from Phoenix, use the `execPeer` method.
40
+
```js
41
+
constXY_NODE_CONNECTOR_ID='ext_x_y'; // Use the same unique ID
42
+
let nodeConnector =global.createNodeConnector(XY_NODE_CONNECTOR_ID, exports);
43
+
44
+
exports.getPWDRelative=asyncfunction(subPath) {
45
+
returnprocess.cwd+'/'+ subPath;
46
+
};
47
+
```
48
+
49
+
With these steps, a `NodeConnector` is set up, enabling two-way communication.
50
+
51
+
## Executing Functions
52
+
53
+
To call a Node.js function from Phoenix, use the `execPeer` method.
15
54
**Example**
16
-
```js// In `x.js` (Phoenix)const fullPath = await nodeConnector.execPeer('getPWDRelative', 'sub/path.html');```To execute a Phoenix function from Node.js and transfer binary data, pass an optional ArrayBuffer.
To execute a Phoenix function from Node.js and transfer binary data, pass an optional ArrayBuffer.
17
61
**Example**
18
-
```js// In `y.js` (Node.js)const { operationDone, buffer } = await nodeConnector.execPeer('modifyImage', {name:'theHills.png'}, imageAsArrayBuffer);```## Event HandlingThe `NodeConnector` object implements all the APIs supported by `utils/EventDispatcher`. You can trigger and listento events between Node.js and Phoenix using the `triggerPeer` and (`on`, `one` or `off`) methods.
The `NodeConnector` object implements all the APIs supported by `utils/EventDispatcher`. You can trigger and listen
70
+
to events between Node.js and Phoenix using the `triggerPeer` and (`on`, `one` or `off`) methods.
19
71
**Example**
20
-
```js// In `y.js` (Node.js)nodeConnector.on('phoenixProjectOpened', (_event, projectPath) => { console.log(projectPath);});nodeConnector.one('phoenixProjectOpened', (_event, projectPath) => { console.log(projectPath + "will be received only once");});```To raise an event from Phoenix to Node.js:
```jsnodeConnector.off('phoenixProjectOpened'); // will switch off all event handlers of that name.```By Default, all events handlers with the eventName is removed when you call `nodeConnector.off(eventName)` fn.To selectively switch off event handlers, please see reference for `utils/EventDispatcher` module.### Handling ArrayBuffer Data in Function ExecutionWhen executing functions that send or receive binary data, ensure that the functions are asynchronous and accept anoptional ArrayBuffer as a parameter. To return binary data, use an object with a `buffer` key.Example of calling a function in Node.js with binary data transfer:
92
+
```js
93
+
nodeConnector.off('phoenixProjectOpened'); // will switch off all event handlers of that name.
94
+
```
95
+
96
+
By Default, all events handlers with the eventName is removed when you call `nodeConnector.off(eventName)` fn.
97
+
To selectively switch off event handlers, please see reference for `utils/EventDispatcher` module.
98
+
99
+
### Handling ArrayBuffer Data in Function Execution
100
+
101
+
When executing functions that send or receive binary data, ensure that the functions are asynchronous and accept an
102
+
optional ArrayBuffer as a parameter. To return binary data, use an object with a `buffer` key.
103
+
104
+
Example of calling a function in Node.js with binary data transfer:
25
105
**Example**
26
-
```js// In `y.js` (Node.js)const { operationDone, buffer } = await nodeConnector.execPeer('modifyImage', {name:'name.png'}, imageArrayBuffer);```### Handling ArrayBuffer Data in Event HandlingUse the `triggerPeer` method to send binary data in events. Include the ArrayBuffer as an optional parameter.Example of sending binary data in an event from Phoenix to Node.js:
Use the `triggerPeer` method to send binary data in events. Include the ArrayBuffer as an optional parameter.
114
+
115
+
Example of sending binary data in an event from Phoenix to Node.js:
27
116
**Example**
28
-
```js// In `x.js` (Phoenix)const imageArrayBuffer = getSomeImageArrayBuffer(); // Get the ArrayBuffernodeConnector.triggerPeer('imageEdited', 'name.png', imageArrayBuffer);```## Caveats- Be cautious when sending large binary data, as it may affect performance and memory usage. Transferring large data is fully supported, but be mindful of performance.- Functions called with `execPeer` and `triggerPeer` must be asynchronous and accept a single argument. An optional second argument can be used to transfer large binary data as an ArrayBuffer.For more event handling operations and details, refer to the documentation for the `utils/EventDispatcher` module.
117
+
```js
118
+
// In `x.js` (Phoenix)
119
+
constimageArrayBuffer=getSomeImageArrayBuffer(); // Get the ArrayBuffer
Creates a new node connector with the specified ID and module exports.Returns a NodeConnector Object (which is an EventDispatcher withadditional `execPeer` and `triggerPeer` methods. `peer` here means, if you are executing `execPeer`in Phoenix, it will execute the named function in node side, and vice versa. You can right away startusing `execPeer`, `triggerPeer`(to send/receive events) APIs without waiting to check if theother side nodeConnector is created.Note: If the NodeConnector has not been created on the other end, requests made with `execPeer` or`triggerPeer` will be temporarily queued for up to 10 seconds to allow time for the connector to be created.If the connector is not created within this timeout period, all queued `execPeer` requests will be rejected,and all queued events will be dropped. It is recommended to call the `createNodeConnector` API on both endswithin a timeframe of less than 10 seconds(ideally same time) for seamless communication.- execPeer: A function that executes a peer function with specified parameters.- triggerPeer: A function that triggers an event to be sent to a peer.- Also contains all the APIs supported by `utils/EventDispatcher` module.
142
+
Creates a new node connector with the specified ID and module exports.
143
+
144
+
Returns a NodeConnector Object (which is an EventDispatcher with
145
+
additional `execPeer` and `triggerPeer` methods. `peer` here means, if you are executing `execPeer`
146
+
in Phoenix, it will execute the named function in node side, and vice versa. You can right away start
147
+
using `execPeer`, `triggerPeer`(to send/receive events) APIs without waiting to check if the
148
+
other side nodeConnector is created.
149
+
150
+
Note: If the NodeConnector has not been created on the other end, requests made with `execPeer` or
151
+
`triggerPeer` will be temporarily queued for up to 10 seconds to allow time for the connector to be created.
152
+
If the connector is not created within this timeout period, all queued `execPeer` requests will be rejected,
153
+
and all queued events will be dropped. It is recommended to call the `createNodeConnector` API on both ends
154
+
within a timeframe of less than 10 seconds(ideally same time) for seamless communication.
155
+
156
+
- execPeer: A function that executes a peer function with specified parameters.
157
+
- triggerPeer: A function that triggers an event to be sent to a peer.
158
+
- Also contains all the APIs supported by `utils/EventDispatcher` module.
43
159
44
160
**Kind**: inner method of [<code>NodeConnector</code>](#module_NodeConnector)
45
161
**Returns**: <code>Object</code> - - A NodeConnector Object. Also contains all the APIs supported by `utils/EventDispatcher` module.
0 commit comments