Coder Social home page Coder Social logo

next-post-message's Introduction

next-post-message

npm version npm downloads bundle JSDocs License

🌳 Tiny & elegant & better windows post message,simple bnd powerful.

Npm Example

Install the package

pnpm install next-post-message

Basic Usage

Sender Window (windowA)

Sending messages has never been easier:

import { Npm } from 'next-post-message'

const tgtWin = document.getElementById('iframeB').contentWindow
const npm = new Npm({ channel: '/chat' })

const { answer } = npm.post('Hello!', tgtWin)

answer
  .then(res => console.log('Received:', res))
  .catch(err => console.error('Error:', err))

Receiver Window (windowB)

In the receiver window, set up a handler to receive messages and send responses:

import { Npm } from 'next-post-message'

const npm = new Npm({ channel: '/chat' })

npm.onReceive(async (msg) => {
  console.log('Received :', msg)
  return 'Hello back'
})

Advanced Usage

🚀 PostMan

Let's be honest, specifying the target window every time you send a message can get tedious.

That's where the PostMan class comes in. It helps manage message sending with custom options and a pre-specified target window.

Sender Window (windowA)

import { Npm } from 'next-post-message'

const tgtWin = document.getElementById('iframeB').contentWindow
const npm = new Npm({ channel: '/detail/blog' })

const postMan = npm.createPostMan(tgtWin, {
  maxWaitTime: 20000,
  enableDebug: true
})

const { answer: answer1 } = postMan.post('Hello through PostMan')
const { answer: answer2 } = postMan.post('Hello again through PostMan')

Receiver Window (windowB)

import { Npm } from 'next-post-message'

const npm = new Npm({ channel: '/detail/blog', enableDebug: true })

npm.onReceive(async (msg) => {
  console.log('Received in:', message)
  return 'Hello back'
})

🚀 GetMan

Using Multiple GetMan Instances for Listening Different Channels

Receiver Window (windowB)

import { Npm } from 'next-post-message'

const npm = new Npm({ enableDebug: true })

const getMan = npm.createGetMan({ channel: '/chat' })
getMan.onReceive(msg => console.log('Chat:', msg))

const getMan2 = npm.createGetMan({ channel: '/update' })
getMan2.onReceive(msg => console.log('Update:', msg))

Hope these examples help you get started quickly and make the most of NextPostMessage!

License

MIT License © 2023-PRESENT leizhenpeng

next-post-message's People

Contributors

leizhenpeng avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

next-post-message's Issues

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.