Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
This library is built on 'postMessage' API to reduce painful communication codes.
If you are familiar with socket.io
, you already know how to use this package.
socket.io
is based on WebSocket.
msgio
is based on postMessage.
Occasionally, we need to build web apps with iframes agross different domains. Communication between these iframes always make us uncomfortable .Using this library less pain you will suffer.
All examples are in the 'examples' directory in this repository.
npm install --save msgio
<script src="//<path to msgio>/msgio.min.js"></script>
<script>
console.log(window.msgio); // Iframe, Guest
// For detail usage, see below...
</script>
import {Iframe} from 'msgio';
const iframe = document.getElementById('my-iframe');
const onLoad = e => {
const io = new Iframe(e.target);
io.on('connect', socket => {
// listening some events from the guest
socket.on('my_custom_event', data => {
// data: {x: 1, y: 2}
// any code
});
// emit events to the guest
socket.emit('greeting', 'hello world!');
// expose functions to the guest
socket.func('my_sync_fn', (a1, a2, ...rest) => {
// a1: 1
// a2: 2
// rest: 3, 4, 5
// any code
// you can throw as usual!
});
// you can expose aysnc functions too.
socket.func('my_async_fn', (a1, a2, ...rest) => {
// a1: 'a'
// a2: 'b'
// rest: 'c', 'd', 'e'
return new Promise((resolve, reject) => {
// any code
});
});
// call some functions the guest provides.
socket.call('hi', 'guest', '!')
.then(result => {
// as you call the 'hi' function with args 'guest' and '!'
// the resule may be 'hello host!', who knows!
// any code
})
.catch(error => {
// the guest may throw error to you
});
});
};
iframe.addEventListener('load', onLoad);
import {Guest} from 'msgio';
// You must provide the host's origin. It's all.
const io = new Guest({host: 'http://localhost:8080'});
// Like in your main app
io.on('connect', socket => {
// the host will use this to set the <iframe>'s width and height.
io.resize({width: 400, height: 600});
// listening some events from the host
socket.on('greeting', data => {
// data: 'hello world!'
// any code
});
// emit events to the guest
socket.emit('my_custom_event', {x: 1, y: 2});
// call functions from the host.
socket.call('my_sync_fn', 1, 2, 3, 4, 5)
.then(result => {
// any code
})
.catch(error => {
// catch the error
});
socket.call('my_async_fn', 'a', 'b', 'c', 'd', 'e')
.then(result => {
// any code
})
.catch(error => {
// catch the error
});
socket.func('hi', (a1, a2, a3) => {
// a1: 'guest'
// a2: '!'
// a3: undefined
// any code
// return 'hello host!'
});
});
Not implemented yet!
Not implemented yet!
constructor(dom: HTMLIFrameElement)
The dom provied must be a iframe node.
on: ('connect', callback: (socket: Socket) => void) => void
constructor(option: {host:string})
Pass the origin string. eg: http://example.com:8080
on: ('connect', callback: (socket: Socket) => void) => void
resize: (size: {width: number, height: number}) => void;
on: (event: string, callback: (...args: any) => void) => void
emit: (event: string, payload: any) => void
func: (fname: string, callback: (...args: any) => any) => void
call: (fname: string, ...args: any) => Promise<any>
FAQs
This library is built on 'postMessage' API to reduce painful communication codes.
The npm package msgio receives a total of 2 weekly downloads. As such, msgio popularity was classified as not popular.
We found that msgio demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.