Code Monkey home page Code Monkey logo

pusher_iss's Introduction

Realtime International Space Station API with AngularJS

Image 2

It's my first week at Pusher and, intrigued by Pusher's realtime Reddit feed, I wanted to have a go at making my first Websockets API. Being a huge fan of AngularJS, I also wanted to see for myself how simple it is to make the framework realtime with Pusher's new Angular library.

NASA, with space always being one of my great fascinations, seemed a natural place to turn to. Upon visiting NASA's data bank, I found that Open-Notify.org had crunched NASA's raw International Space Station location data into a JSON API. Perfect, I thought, as it would be a chance to get some frequently updated data and stream it with the power of Websockets.

Using The API With AngularJS

Firstly, we need to include AngularJS, Pusher, and Pusher-Angular within our HTML file.

<!-- AngularJS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>

<!-- pusher-js -->
<script src="//js.pusher.com/2.2/pusher.min.js"></script>

<!-- pusher-angular -->
<script src="//cdn.jsdelivr.net/angular.pusher/latest/pusher-angular.min.js"></script>

Now in our javascript we need to set up an Angular application and controller.

// Inject 'pusher-angular' as a dependency
angular.module('MyApp', ['pusher-angular'] )

// Then inject the $pusher service into MyCtrl

.controller('MyCtrl', ['$scope', '$pusher', function($scope, $pusher){
	// code goes here...
}]);

Within our controller, setting up our Pusher client to talk to the API should be fairly straightforward. Simply instantiate a new Pusher object (with the key for the ISS API), and pass it into the $pusher service.

var client = new Pusher('fa15651bc1ad6c916fc7');
var pusher = $pusher(client);

Now let's have our Pusher client subscribe to the International Space Station channel:

var issChannel = pusher.subscribe('iss-channel');

Now we can log out the data from the 'new-location' event triggered by the API in realtime:

issChannel.bind('new-location', function(iss){
	console.log(iss);
});

In the console, this will print out:

{
	"timestamp":1414058029,
	"message":"success",
	"iss_position":
		{
			"latitude":-31.90622125481199,
			"longitude":-25.751114321984957
		}
}

And there you have it: a live feed of the whereabouts of the International Space Station using Pusher and AngularJS.

##Try The Demo And Build Something!

Check out Where Is The ISS? to have a peek at the demo. It tracks the coordinates of the International Space Station on a map, while below you can see NASA's live stream of shots taken by cameras on the exterior of the station.

Image 1

Feel more than free to make your own application with this API and let us know when you have. We'd love to see what more you can do with it!

If you're an Angular-lover and are thinking of combining it with Pusher, head on over to the Pusher-Angular Github Page to find more ways to power your app with realtime.

pusher_iss's People

Contributors

jpatel531 avatar

Watchers

James Cloos avatar

pusher_iss's Issues

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.