chore: createNodeConnector API returns ready to use node connector instead of promise #1242
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NodeConnector
Node Connector Communication Module
This 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 andy.js
in Node.js, follow these steps:Create
NodeConnector
in Phoenix (x.js
)Create
NodeConnector
in Node.js (y.js
)With these steps, a
NodeConnector
is set up, enabling two-way communication.Executing Functions
To call a Node.js function from Phoenix, use the
execPeer
method.To execute a Phoenix function from Node.js and transfer binary data, pass an optional ArrayBuffer.
Event Handling
The
NodeConnector
object implements all the APIs supported byutils/EventDispatcher
. You can trigger and listento events between Node.js and Phoenix using the
triggerPeer
and (on
,one
oroff
) methods.To raise an event from Phoenix to Node.js:
To Switch off events
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 Execution
When executing functions that send or receive binary data, ensure that the functions are asynchronous and accept an
optional 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:
Handling ArrayBuffer Data in Event Handling
Use 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:
Caveats
data is fully supported, but be mindful of performance.
execPeer
andtriggerPeer
must be asynchronous and accept a single argument. An optionalsecond 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.createNodeConnector
Creates a new node connector with the specified ID and module exports.
Returns a NodeConnector Object (which is an EventDispatcher with
additional
execPeer
andtriggerPeer
methods.peer
here means, if you are executingexecPeer
in Phoenix, it will execute the named function in node side, and vice versa. You can right away start
using
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
ortriggerPeer
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.
utils/EventDispatcher
module.Parameters
nodeConnectorID
string The unique identifier for the new node connector.moduleExports
Object The exports of the module that contains the functions to be executed on the other side.Returns {execPeer: function, triggerPeer: function, trigger: function, on: function, off: function, one: function} A NodeConnector Object. Also contains all the APIs supported by
utils/EventDispatcher
module.isNodeAvailable
Checks if Node.js Engine is available.
Returns boolean Returns true if Node.js Engine is available.