Simple node and rust script to achieve an easy to use bridge between rust and node.js

Overview

Node-Rust Bridge

Simple rust and node.js script to achieve a bridge between them. Only 1 bridge can be initialized per rust program. But node.js can have many bridges.

Installation

Add the following line to your Cargo.toml under [dependencies]

node_bridge = "1.0.2"

Node.js installation:

$ npm install rustlang-bridge

Usage/Examples

use node_bridge::NodeBridge;

#[tokio::main]
async fn main() {
    let bridge = NodeBridge::new();
    bridge.register("addition", add, None);
    bridge.register_async("find_longer", find_longer, Some("Variable to pass to the function"));
    assert_eq!(bridge.receive("channel_a").await.unwrap(), "Sent this from node!");
    bridge.send("channel_foo", "bar").ok();
    bridge.wait_until_closes().await;
}
 
fn add(params: Vec<i32>, _: Option<()>) -> i32 {
    params[0] + params[1]
}
 
async fn find_longer(params: Vec<String>, pass_data: Option<&str>) -> String {
    assert_eq!(pass_data, Some("Variable to pass to the function"));
    if params[0].len() > params[1].len() {
        return params[0].to_string();
    }
    params[1].to_string()
}

Handling in node.js:

import RustBridge from "rustlang-bridge";
 
const bridge = new RustBridge("/path/to/rust_executable");
await new Promise(resolve => setTimeout(resolve, 500)); // wait for the functions to initialize
console.log(await bridge.addition(10, 20)); // "30"
console.log(await bridge.find_longer("foo", "longer_foo")); // "longer_foo"
bridge.on("channel_foo", data => {
    console.log(data); // "bar"
});
bridge.send("channel_a", "Sent this from node!");

Contributing

Contributions are always welcome!

You might also like...
A simple test on interacting between languages with JS and Rust.

NODE-JS-RUST-FFI A simple test on interacting between languages with JS and Rust. How to run Requirements nodejs rustc + cargo Compile First compile t

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native

🖌 egui: an easy-to-use GUI in pure Rust 👉 Click to run the web demo 👈 egui is a simple, fast, and highly portable immediate mode GUI library for Ru

Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

A node and runtime configuration for polkadot node.
A node and runtime configuration for polkadot node.

MANTA NODE This repo is a fresh FRAME-based Substrate node, forked from substrate-developer-hub/substrate-node-templte 🚀 It links to pallet-manta-dap

An easy-to-use, high-performance Interledger implementation written in Rust

Interledger implementation in Rust 💸 Requirements All crates require Rust 2018 edition and are tested on the following channels: stable Connecting to

An easy-to-use generic trainer for Linux, written in Rust.
An easy-to-use generic trainer for Linux, written in Rust.

An easy-to-use generic trainer for Linux written in Rust, with basic memory hacking features. Use it for your games or applications where needed, there is also the tuxtraind daemon which will probe for processes and apply matching trainers automatically.

A modern, portable, easy to use crypto library.
A modern, portable, easy to use crypto library.

Sodium is a new, easy-to-use software library for encryption, decryption, signatures, password hashing and more. It is a portable, cross-compilable, i

An easy-to-use CLI tool to recover files from zfs snapshots

zfs-undelete an easy-to-use cli tool to recover files from zfs snapshots Usage Use zfs-undelete file-to-restore. Works for file and folders. By defa

Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample).

ldk-node-sample Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample ). Installation git clone https://github.com/op

Owner
Pure
Discord: Pure#8181
Pure
Terabethia - A Bridge and Messaging Protocol between Ethereum and the Internet Computer.

Terabethia - A Bridge Between Ethereum & the Internet Computer Terabethia is a bridge between Ethereum & the Internet Computer that contracts in both

Psychedelic 36 Dec 26, 2022
A value transfer bridge between the Monero blockchain and the Secret Network.

Secret-Monero-Bridge A value transfer bridge between the Monero blockchain and the Secret Network. Proof-of-Concept Video Demonstration: https://ipfs.

null 28 Dec 7, 2022
A bridge between different serde implementations.

serde-bridge   Bridge between serde types Quick Start use anyhow::Result; use serde_bridge::{from_value, into_value, FromValue, IntoValue, Value}; fn

Xuanwo 14 Nov 24, 2022
The Phala Network Blockchain, pRuntime and the bridge.

Phala Blockchain Phala Network is a TEE-Blockchain hybrid architecture implementing Confidential Contract. This repo includes: node/: the main blockch

Phala Network 314 Jan 6, 2023
🤖CyberAI is designed to bridge the world of Cyberpunk 2077 and the power of OpenAI's AI technology.

CyberAI ?? Welcome to the CyberAI project! This plugin for Cyberpunk 2077 enables integration between the videogame and OpenAI API, opening a world of

Kirill Kuzin 19 Jul 28, 2023
Filecoin to substrate bridge

filecoindot A Substrate pallet to bridge from Filecoin to any blockchain built using the Substrate framework, including Polkadot parachains. How to in

ChainSafe 23 Nov 23, 2022
Cross-chain bridge message delivery network. We are hiring, [email protected]

Introduction Implementation of a https://darwinia.network node in Rust based on the Substrate framework. This repository contains runtimes for the Dar

Darwinia Network 225 Nov 8, 2022
interBTC A trust-minimized bridge from Bitcoin to Polkadot.

interBTC A trust-minimized bridge from Bitcoin to Polkadot. Explore the specification » Report Bug · Request Feature This repository is hosted on GitH

Interlay 192 Dec 27, 2022
Simple to use CLI tool that makes encryption easy! Written in Rust.

?? eme: Encryption Made Easy an extremely simple AES-256 encryption tool written in Rust Usage: # To encrypt: eme --encrypt secret.png # To decrypt: e

null 5 Jan 3, 2023
⚡️ NFT Snapshot script written in Rust for blazingly fast speeds ⚡️

⚡️ NFT SNAPSHOT ⚡️ This project uses rust's blazingly fast performance along with the ethers-rs library to read blockchain state. Setup You will have

Trevor Johnson 4 Oct 14, 2022