tabtalk v1.0.4
tabtalk
Simple, secure cross-origin communication between browser tabs
Table of contents
Usage
import createTab from 'tabtalk';
const tabtalk = createTab({
onChildCommunication(message) {
console.log('message from child', message);
},
onChildRegister(childTab) {
tabtalk.sendToChild(childTab.id, 'Hello my child!');
},
onRegister() {
console.log('ready to go!');
},
});
tabtalk.open({url: 'http://www.example.com'});Messages are sent by postMessage, allowing for cross-origin usage (with the use of custom config properties), and the data in the message is encrypted with krip under the hood.
createTab
createTab([config: Object]): TabTalkCreates a new TabTalk instance where the window it is executed in (itself) is the tab. The instance returned allows you to open children, and will automatically glean its parent if it was opened from another TabTalk instance.
config
createTab optionally accepts a config object which has the following shape:
{
// custom key to be used by krip for encryption / decription
// HIGHLY RECOMMENDED
encryptionKey: string,
// called when child closes
onChildClose: (child: TabTalk) => void,
// called when child sends message to self
onChildCommunication: (message: any, eventTime: number) => void,
// called when child registers with parent
onChildRegister: (child: TabTalk) => void,
// called when self is closed
onClose: () => void,
// called when parent is closed
onParentClose: () => void,
// called when parent sends message to self
onParentCommunication: (message: any, eventTime: number) => void,
// called when self registers
onRegister: (self: TabTalk) => void,
// the origin to use for cross-tab validation
origin: string = window.origin || document.domain || '*',
// the delay to wait when a tab doesn't check in but has already checked in
pingCheckinBuffer: number = 5000,
// the delay to wait before the next check if tab is checking in
pingInterval: number = 5000,
// the delay to wait when a tab doesn't check in and has never checked in
registrationBuffer: number = 10000,
// should the tab be removed as a child when closed
removeOnClose: boolean = false,
}TabTalk instance API
open
Opens a new child TabTalk instance and stores it as a child in the opener's TabTalk instance. Resolves the child instance.
open(options: Object): Promise<TabTalk>Example:
const openChild = async config => {
const child = await tabtalk.open({
config,
url: 'http://www.example.com/',
});
return child;
};open() accepts the following options:
{
// the config to pass through to createTab for the child
config: Object = self.config,
// the destination to open
url: string,
// any windowFeatures to apply to the opened window
windowFeatures: string,
}For more information on windowFeatures, see the MDN documentation.
close
Closes the TabTalk instance, or if an ID is passed then closes that matching child TabTalk instance.
close([id: string]) => voidExample:
tabtalk.close(); // closes self
tabtalk.close(child.id); // closes specific childsendToChild
Send data to a specific child. The promise resolved is empty, it's mainly to have control flow over a successful message sent.
sendToChild(id: string, data: any): Promise<null>Example:
tabtalk.sendToChild(child.id, {
message: 'Special message',
total: 1234.56,
});NOTE: Data can be anything that is serializable by JSON.stringify.
sendToChildren
Send data to all children. The promise resolved is empty, it's mainly to have control flow over a successful message sent.
sendToChildren(data: any): Promise<null>Example:
tabtalk.sendToChildren({
message: 'Special message',
total: 1234.56,
});NOTE: Data can be anything that is serializable by JSON.stringify.
sendToParent
Send data to the parent. The promise resolved is empty, it's mainly to have control flow over a successful message sent.
sendToParent(data: any): Promise<null>Example:
tabtalk.sendToParent({
message: 'Special message',
total: 1234.56,
});NOTE: Data can be anything that is serializable by JSON.stringify.
Support
Support is mainly driven by krip support, as postMessage works correctly back to IE8.
- Chrome 37+
- Firefox 34+
- Edge (all versions)
- Opera 24+
- IE 11+
- Requires polyfills for
Promise,TextDecoder, andTextEncoder
- Requires polyfills for
- Safari 7.1+
- iOS 8+
- Android 5+
Development
Standard stuff, clone the repo and npm install dependencies. The npm scripts available:
build=> runrollupto build browser and node versions- standard versions to top-level directory, minified versions to
distfolder
- standard versions to top-level directory, minified versions to
clean:dist=> runrimrafondistfolderdev=> runwebpackdev server to run example app / playgrounddist=> runclean:dist,buildlint=> runeslintagainst all files in thesrcfolderlint:fix=> runlint, fixing issues when possibleprepublish=> runsprepublish:compilewhen publishingprepublish:compile=> runlint,test:coverage,buildstart=> rundevtest=> run AVA test functions withNODE_ENV=testtest:coverage=> runtestbut withnycfor coverage checkertest:watch=> runtest, but with persistent watcher