Git Product home page Git Product logo

pusher-js-auth's Introduction

Pusher plugin for authentication

Pusher plugin for batching auth requests in one HTTP call.
When subscribing to multiple private- and presence channels at once, your browser has to make an HTTP request for each channel. This plugin enables you to process multiple channel authentications within one request.

Prerequisites

This is a plugin for the official Pusher JavaScript library and compatible with the latest 4.1.x release. Make sure you have a working implementation up and running.

Notice: This version is not compatible with Pusher 4.0 and older. Please use version 2.0.0 of this plugin with older Pusher versions.

Documentation and configuration options are explained at the Pusher-js Github page

Usage

Load the plugin after including the Pusher library

<script src="//js.pusher.com/4.2/pusher.min.js"></script>
<script src="lib/pusher-auth.js"></script>

This plugin is also available on npm and bower:

npm install pusher-js-auth
bower install pusher-js-auth

Configuration

This plugin comes with a few extra configuration parameters. The whole list is available at the Pusher-js Github page

var pusher = new Pusher(API_KEY, {
    authorizer: PusherBatchAuthorizer,
    authDelay: 200
});

authorizer (Function)

Pass the function exposed by this plugin here. It is exposed as a module export when using AMD or CommonJS, and as the PusherBatchAuthorizer global otherwise.

authDelay (Number)

Optional, defaults to 0. Delay in milliseconds before executing an authentication request. The value can be as low as 0 when subcribing to multiple channels within the same event loop. Please note that the first authentication request is postponed anyway until the connection to Pusher succeeds.

Server side authentication

Your authentication endpoint should be able to handle batched requests.

Incoming post data

socket_id   	  00000.0000000
channel_name[0]	  private-a
channel_name[1]	  private-b

Expected output

{
    "private-a": {
        "status": 200, // HTTP status codes, optional on success
        "data": {
            "auth": "xxxxxx:xxxxxxxxxxxxx"
        }
    },
    "private-b": {
        "status": 200,
        "data": {
            "auth": "xxxxxx:xxxxxxxxxxxxx"
        }
    },
    "private-c": {
        "status": 403
    }
}

Use one of the server libraries to do most of the hard work.

Example implementation

Copy app_key.example.js and app_key.example.php to app_key.example.xx and fill in your own Pusher data. Create a small PHP server and run index.html with your browser's debug console active.

pusher-js-auth's People

Contributors

brettjonesdev avatar dirkbonhomme avatar marknadig avatar stof avatar

Watchers

 avatar

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.