Giter Club home page Giter Club logo

postie.js's Introduction

Postie.js

Postie makes it easy send rich (JSON) data back and forth between a web page and an embedded iframe.

Under the hood it uses the HTML5 postMessage API.

About Postie.js

Postie.js abstracts away the native postMessage API for a number of reasons:

  • HTML5 postMessage doesn't support sending complex data structures; it can only send Strings. Postie.js allows you to send any valid JSON, including simple Strings or Integers.
  • Postie.js allows you to send data and receive the response in a callback function, providing a much more convenient request/response communication format.

When would I use it?

It's ideal for creating bookmarklets and scripts that are injected into 3rd party sites and need to communicate with your app.

How does it work?

Postie.js comes in two parts, the Client and the Server. The Server instance is also passed a JSON object (called the Receiver) that defines the available endpoints.

  1. The Client enables sending of data to the Server (in another frame) and handles the response sent back.
  2. The Server listens for requests from the Client, passes the request to a "Receiver" object and sends back the result to the client.
  3. The Receiver object is simply a JSON object of key-value pairs where the key is the name of the endpoint for the client to call, and the value is a function that takes any data send by the client and provides whatever response you need.

Installation

Add the postie.js file to the pages you neec the client and server on.

<script src="postie.js"></script>

Usage

Enough talk, time for an example:

Step 1) Create a list of endpoints for the server

This should be defined in your background page (2).

var myReceiver = {

  // First endpoint gets a User by an ID and returns a JSON representation of it.
  // id - This is the user id sent from the client
  // sendResponse - this is a function provided by the Postie.js server. Call it, passing any
  // data you want. This is send the response back to the client
  getUser: function(id, sendResponse) {
    $.get('/users/' + id + '.json',
      success: function(data, status, xhr) {
        sendResponse(data);
      }
    );
  },

  // Second endpoint simply returns the string of 'foo'.
  getFoo: function(sendResponse) {
    sendResponse('foo');
  }
};
Step 2) Create the server and start listening

This should be defined in your background page / iframe

var server = new Postie.Server(receiver: myReceiver)
server.listen()
Step 3) Create the client

This should be defined in your main window. Pass the URL of where the Server instance is listening. The Client will create an iframe inside the DOM. Run all your calls to the Server inside the ready callback to ensure the iframe has loaded.

var client = new Postie.Client('http://localhost:8081/bookmarklet/background.html');
client.ready(function() {
  // start calling endpoints here
});
Step 4) Call an endpoint and receive the response

This should be defined in your main window

client.ready(function() {
  console.log( client._endpoints );
  // => ['getUser', 'getFoo']

  client.getUser(123, function(user) {
    console.log('Got user:', user);
  });
  // => Got user: { id: 123, name: 'J Bloggs', favColor: 'green' }

  client.getFoo(function(data) {
    console.log('Got foo:', data);
  });
  // => Got foo: 'foo'

});

postie.js's People

Contributors

paulspringett avatar

Stargazers

Inno avatar Philip Manavopoulos avatar Jim Newbery avatar  avatar

Watchers

 avatar James Cloos avatar

Forkers

inno-v

postie.js's Issues

New request / response format

# Client Application
postie = new Postie.Client(server: '#server-iframe', methods: ['getUser', 'getContent'])

postie.ready ->
  postie.getUser (response) ->
    @user = new User(response)

  postie.getContent '/pages/foo', (response) ->
  @page = new Page(response)

# Server Background
postie = new Postie.Server(receiver: new Receiver)
postie.listen()


class Receiver
  getUser: (request, sendResponse) ->
    sendResponse(foo: "bar")

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.