-
-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node integration layer and node connector #1241
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 tasks
Thank You for making this pull request. |
Thank You for making this pull request. |
Thank You for making this pull request. |
Thank You for making this pull request. |
Quality Gate passedThe SonarCloud Quality Gate passed, but some issues were introduced. 4 New issues |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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. Ensure thatnodeConnector
is set before using it.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
andon
methods.To raise an event from Phoenix to Node.js:
To Switch off events
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
nodeConnectedPromise
is resolved before using thenodeConnector
to avoid potential issuesrelated to communication readiness.
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 promise that resolves to an 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.
The promise will be resolved only after a call to
createNodeConnector
on the other side with thesame
nodeConnectorID
is made. This is so that once the promise is resolved, you can right away starttwo-way communication (exec function, send/receive events) with the other side.
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 Promise A promise that resolves to an NodeConnector Object.