Code Monkey home page Code Monkey logo

ember-pusher's Introduction

Ember Pusher Build Status

A library for declaratively managing connections to Pusher channels and events in your Ember application.

You are able to connect to different channels and events declaratively as the user traverses through your application. The interface to event handlers are natural methods on your controllers. In fact: The full event bubbling framework is available to the pusher initiated events.

Download

Is it good?

Yes

Is it production ready?

I guess? Ship it!

How do I use this thing?

Good question!

First things first: Stick your pusher connection options on your Application object. Anything you pass into the connection hash will be passed in as options to the pusher connection. This is a good place for auth params, and things like that. You can get a list of all the options from Pusher's API.

App = Ember.Application.create({
  PUSHER_OPTS: { key: 'foo', connection: {} }
});

Next, for any controllers that you want to catch pusher events on:

  1. Extend EmberPusher.Bindings.
  2. Define PUSHER_SUBSCRIPTIONS where the keys are channel names and the values are arrays of events for the channel. If you have dynamic channel names or events, you can totally just construct your PUSHER_SUBSCRIPTIONS hash in init() of your controller. Private channels are fine.
  3. Implement your event handlers on the controller.
Example Controller:
var YourController = Em.Controller.extend(EmberPusher.Bindings, {
  PUSHER_SUBSCRIPTIONS: {
    craycray: ['event-one', 'event-two'],
    anotherone: ['event-three']
  },
  actions: {
    eventOne: function(){ console.log("eventOne is working!"); },
    eventTwo: function(){ console.log("eventTwo is working!"); }
    eventThree: function(){ console.log("eventThree is working!"); }
  }
});

Note: The event names have camelize() called on them, so that you can keep your controller's methods looking consistent. Event handlers are tracked and torn down when a controller is destroyed.

That's about it! When events come in, they should be triggered on the listening controllers. It should be noted that event bubbling will all work as expected, so you can actually implement your handlers wherever suits your needs best.

Have fun! Certainly let me know if you see any bugs.

FAQ

question My event's aren't firing! :'(

Are you sure you've got the right event name on your controller? Do an Em.String.camelize('foo-bar') on your event name. That's what you should have implemented on your controller. Did you make sure to extend EmberPusher.Bindings on the controller(s) you want to catch events on?

question Can I connect to a private channel!?

Yes.

question What versions of Ember are supported!?

~>1.0.0

question Can I bind to connection events!?

Indeed! I do this in my apps to attach the pusher socket ID as a header to xhr requests. It would look like this:

App.MyController = Ember.Controller.extend(EmberPusher.Bindings, {
  needs: ['pusher'],

  PUSHER_SUBSCRIPTIONS: {
    my-channel: ['pusher:subscription_succeeded']
  },

  actions: {
    'pusher:subscriptionSucceeded': function() {
      console.log("Socket ID:", this.get('controllers.pusher.socketId'));
    }
  }
});

Running the tests

grunt test - Runs Mocha tests through PhantomJS

grunt server - Run tests through a browser. Visit http://localhost:8000/test.

Building

grunt build - build 'er

Contributors

@wycats - Architectural advise.

ember-pusher's People

Contributors

gilgen avatar

Watchers

James Cloos avatar Lucas Holmquist avatar  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.