Coder Social home page Coder Social logo

rajnandan1 / ruto Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 79 KB

Ruto is a library that streamlines communication between browser window to iframes or popups. It uses client server design pattern

Home Page: https://rajnandan1.github.io/ruto/index.html

License: MIT License

HTML 65.43% JavaScript 1.71% TypeScript 30.62% CSS 2.25%
iframe postmessage-async-await postmessage-library postmessage-promise

ruto's Introduction

Ruto

Ruto is a lightweight(4KB), fast and easy-to-use JS library that streamlines the communication between parent and child window(iframe/popup).

It uses client-server design pattern to communicate between parent and child window. Any window can become the client or the server depending on who wants to send. It abstracts out the complications of postMessage API and provides a simple API to send and receive messages.

Scenarios where it can be used

  1. Parent window wants to send a message to child window and wants to wait for the response from the child window.
  2. Parent window wants to send a message to child window and expects a reply within x seconds.

Demo

Table of Contents

Installation

CDN

<script src="https://cdn.jsdelivr.net/gh/rajnandan1/ruto/dist/ruto.min.js"></script>

ES6

npm i @rajnandan1/ruto

API

The library exposes two methods send and receive.

send

The send method is used to send a message from parent window to child window or vice versa. It returns a promise which resolves with the response from the child window.

send(route: string, node: Window | HTMLIFrameElement, message: string, options: WindowMQOptions): Promise<string>

route

The route is a unique identifier for the message. It has three parts

  • origin: The origin of the child window. Example if iframe or popup is located at http://example.com/iframe.html, then the origin will be http://example.com
  • type: The type of the window communication
    • parent-to-iframe: When you want to send a message from parent to iframe
    • parent-to-window: When you want to send a message from parent to popup
    • iframe-to-parent: When you want to send a message from iframe to parent
    • window-to-parent: When you want to send a message from popup to parent
  • topic: The topic of the message. Can be any string

Example: http://example.com/parent-to-iframe/sometopic

node

The node is the child window to which the message is to be sent. It can be either a window object or an iframe element. Example: document.getElementById('iframe') or window.open('popup.html', 'popup', 'width=600,height=400)

message

The message is the data that is to be sent to the child window. It has to be a string. If you want to send JSON, you have to stringify it before sending.

options

The options is an object that can have the following properties. It is optional

  • timeout: The time in milliseconds to wait for the response from the child window. If the response is not received within the timeout, the promise will be rejected with a timeout error. Example: {timeout: 5000}

receive

The receive method is used to receive a message from a window. It is used by the child window to receive messages from the parent window. It can also be used by parent to receive message from child window. It is a callback function that is called when a message is received.

receive(subpath: string, node: Window | HTMLIFrameElement, callback: (res: Response, message: string) => void)

subpath

The subpath is a unique identifier for the message. It has two parts

  • type: The type of the window communication
    • parent-to-iframe: When you want to send a message from parent to iframe
    • parent-to-window: When you want to send a message from parent to popup
    • iframe-to-parent: When you want to send a message from iframe to parent
    • window-to-parent: When you want to send a message from popup to parent
  • topic: The topic of the message. Can be any string

Important

The subpath should be the same as the route of the sender. If the subpath is different, the message will not be received. It does not need the origin part of the route.

Example: parent-to-iframe/sometopic

node

The node is the window from which the message is to be received. It can be

  • iframe element
  • popup window reference
  • window.parent
  • window.opener

Example: document.getElementById('iframe') or window.parent

callback

The callback is a function that is called when a message is received. It has two parameters

  • response: The response object that is used to send a response back to the sender using response.send
  • message: The message that is sent by the sender. It is a string.

Example:

ruto.receive('http://localhost:3000/parent-to-iframe/sometopic', window.parent, (response, message) => {
    const newMessage = message + ' edited by child';
    return response.send(newMessage);
});

Usage

⭐ Parent to Iframe Example

Parent Window

//<iframe id="http://localhost:3000/somePath/someiframe.html" src="iframe.html" width="100%" height="800" frameborder="0"></iframe>

const options = {
    timeout: 5000,
};
ruto.send('http://localhost:3000/parent-to-iframe/sometopic', document.getElementById('iframe'), 'Your message', options)
    .then((response) => {
        console.log(response); //Your message edited by iframe
    })
    .catch((error) => {
        console.log(error);
    });

Child Window

ruto.receive('/parent-to-iframe/sometopic', window.parent, (response, message) => {
    const newMessage = message + ' edited by iframe';
    return response.send(newMessage);
});

⭐ Parent to Popup Example

Parent Window

//const popup = window.open('popup.html', 'popup', 'width=600,height=400');

const options = {
    timeout: 5000,
};
ruto.send('http://localhost:3000/parent-to-window/sometopic', popup, 'Your message', options)
    .then((response) => {
        console.log(response); //Your message edited by popup
    })
    .catch((error) => {
        console.log(error);
    });

Popup Window

ruto.receive('/parent-to-window/sometopic', window.opener, (response, message) => {
    const newMessage = message + ' edited by popup';
    return response.send(newMessage);
});

⭐ Iframe to Parent Example

Iframe Window

const options = {
    timeout: 5000,
};
ruto.send('http://localhost:3000/iframe-to-parent/sometopic', window.parent, 'Your message', options)
    .then((response) => {
        console.log(response); //Your message edited by parent
    })
    .catch((error) => {
        console.log(error);
    });

Parent Window

ruto.receive('/iframe-to-parent/sometopic', window.parent, (response, message) => {
    const newMessage = message + ' edited by parent';
    return response.send(newMessage);
});

⭐ Popup to Parent Example

Popup Window

const options = {
    timeout: 5000,
};
ruto.send('http://localhost:3000/window-to-parent/sometopic', window.opener, 'Your message', options)
    .then((response) => {
        console.log(response); //Your message edited by parent
    })
    .catch((error) => {
        console.log(error);
    });

Parent Window

ruto.receive('/window-to-parent/sometopic', window.opener, (response, message) => {
    const newMessage = message + ' edited by parent';
    return response.send(newMessage);
});

ruto's People

Contributors

rajnandan1 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.