An open API service indexing awesome lists of open source software.

https://github.com/krasimir/lsbridge

Using local storage as a communication channel
https://github.com/krasimir/lsbridge

Last synced: 6 months ago
JSON representation

Using local storage as a communication channel

Awesome Lists containing this project

README

          

# The Local Storage Bridge

1K library for exchanging messages between tabs in a same browser. It uses the local storage as a communication channel.

> The story behind the library is available [here](http://krasimirtsonev.com/blog/article/Using-Local-Storage-as-a-communication-channel).

## Usage

Add **lsbridge.min.js** to your page:

```html

```

There is a global object `lsbridge` available.

Send messages:

```js
lsbridge.send('my-namespace', { message: 'Hello world!' });
```

Listen for messages:

```js
lsbridge.subscribe('my-namespace', function(data) {
console.log(data); // prints: { message: 'Hello world!'}
});
```

Find out if `localStorage` is available:

```js
console.log(lsbridge.isLSAvailable); // prints "true" or "false"
```

Cancel listeners for specific namespace:

```js
lsbridge.unsubscribe('my-namespace');
```

## Compilation

* Run `npm install` to get UglifyJS installed.
* Run `npm run-script compile` to produce `build/jsbridge.min.js`

## Example

Try it yourself by opening `example/index.html` in a browser.

![lsbridge](http://work.krasimirtsonev.com/git/lsbridge/lsbridge.gif)