Comments (10)
I did something similar a decade ago using localStorage events.
See https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event
from mitt.
Following up.. at least for my iframe scenario, it seems that simply using parent
does the trick.
So I can do window.Bus = mitt();
on the main app and then inside the Vue instance loaded inside the iframe, I can simply do parent.Bus.emit('something')
and that works. I suppose with a bit of tinkering I'll be able to do the inverse too if needed.
This doesn't solve how to use mitt() to communicate between two browser tabs though, if that's even possible.
from mitt.
@vesper8 Communicating between different tabs is something possible with the SharedWorker
web api, from my understanding it is basically a web worker which can share memory across different threads (browser tabs).
Though keep in mind, that the browser support for the SharedWorker api is not that good.
https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker#browser_compatibility
If you come across any other way for communicating accross different tabs (without a browser extension) do share here too!
from mitt.
I was able to get a working implementation to get my event bus to work between the parent app and an iframe by passing the event bus itself to the parent's event bus, which then has a reference to the iframe's event bus. I then store this bus in my store state so it can be easily references anywhere in my app. Bit hacky but works just fine.
As for communicating between two different tabs, I'm using my store state again for this, I was able to achieve this by installing vuex-shared-mutations
which makes the store state accessible across tabs. This is more of a proof of concept as I don't actually need interaction between two tabs, my main goal was to facilitate interaction between an iframe and its parent (both hosted on the same domain). But since I am now able to create watchers on my store's state in different tabs, then I could use that to trigger bus events between tabs.
from mitt.
Hello, you are using the cross-document communication API: window.postmessage (), I wonder if this will solve your problem
from mitt.
@DVSoftware That's the right way to go! Closing since IMO the "storagechange"
event is the best option here (and you can wire it up to mitt if you need).
from mitt.
@vesper8 - did you ever manage to figure out the inverse direction as you noted on 5/16?
I have a wrapper around a vue app (so it can be embedded as a shim inside another web app) and need to emit an event from the parent window (straight html/JS, nothing special), into the vue app frame.
Everything works fine in the vue app frame, but I can't seem to reference anything regarding the emitter in the parent context to even try to emit to the child vue app.
The documentation for Mitt says: You can find the library on window.mitt. but that doesn't seem to be true in my case.
from mitt.
@mbcbus from inside the child iframe try window.parent.mitt
- assuming you're using mitt in the parent iframe
Do note that window.mitt
or window.parent.mitt
will only work for UMD bundles which are imported via script tags.
If you're using ESM or CJS, you will have to manually set the emitter on the window object
from mitt.
@mbcbus from inside the child iframe try
window.parent.mitt
- assuming you're using mitt in the parent iframeDo note that
window.mitt
orwindow.parent.mitt
will only work for UMD bundles which are imported via script tags. If you're using ESM or CJS, you will have to manually set the emitter on the window object
I need to go the opposite direction though (sending model updates from the parent, which is not vue, into the child, which is a vue app).
from mitt.
Following up.. at least for my iframe scenario, it seems that simply using
parent
does the trick.So I can do
window.Bus = mitt();
on the main app and then inside the Vue instance loaded inside the iframe, I can simply doparent.Bus.emit('something')
and that works. I suppose with a bit of tinkering I'll be able to do the inverse too if needed.This doesn't solve how to use mitt() to communicate between two browser tabs though, if that's even possible.
I can't get parent.Bus
, maybe my iframe have a different origin with parent origin.
from mitt.
Related Issues (20)
- Nuxt3 issues ? HOT 2
- Persist events that not yet have a handler HOT 1
- [TypeScript] define events interface HOT 3
- Mitt - some problems HOT 1
- Typescript issue with `moduleResolution: NodeNext` HOT 1
- `dist/mitt.mjs` must not have a sourcemap HOT 5
- mitt extension
- Unpkg CDN Version is down HOT 1
- Using Ag-grid cell renderer with Mitt HOT 1
- Bug: type issue in [email protected]+ with esm HOT 2
- Could we have a minor update? HOT 1
- Doesn't work with typescript 5 moduleResolution set to bundler HOT 11
- vue3 options methods use mitt Invalid HOT 1
- why use sllice & map HOT 2
- Can't pass multiple arguments with emit HOT 2
- How to declare type of event parameter when listen for event HOT 1
- error TS2349: This expression is not callable when using "module": "NodeNext". HOT 5
- Uncaught TypeError: (0 , mitt__WEBPACK_IMPORTED_MODULE_5__.default)(...) is not a function HOT 1
- Emit issue on Vue 3. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mitt.