nprapps/newscast.js

Name: newscast.js

Owner: NPR visuals team

Description: A library to radically simplify Chromecast web app development.

Created: 2015-01-27 21:58:29.0

Updated: 2017-07-10 02:14:55.0

Pushed: 2015-05-18 16:40:11.0

Homepage: http://apps.npr.org/newscast/docs/

Size: 779

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Newscast.js

npm

Newscast is a library to radically simplify Chromecast web app development.

Installation
m install newscast.js
Creating a Newscast

Newscast is optimized for a very simple use-case:

For this case, you only need the following code to create a Chromecast-ready Javascript app:

Newscast.Newscast({
'namespace': 'urn:x-cast:newscast.simple',
'appId': 'E07BAAC9',
'debug': true,
'onReceiverCreated': onCastReceiverCreated,
'onSenderCreated': onCastSenderCreated,
'onSenderReady': onCastSenderReady,
'onSenderStarted': onCastSenderStarted,
'onSenderStopped': onCastSenderStopped

There are three pieces of configuration:

There are also five callbacks you will need to implement:

When the user clicks your “start casting” button, call `Sender.startCasting(). To stop the cast, call Sender.stopCasting().

That's it.

See the simple example for a working example.

Registering your application

Before you can test your application, you'll need to be registered as Chromecast developer with Google (this costs $5), register your Chromecast device and register your app. Google's documentation for this process is here.

Use the following configuration when you register your app:

The “newscast-receiver=true” is part of the magic that makes Newscast work with only a single page.

Note: SSL (https://) is required for published applications, but you can test without it.

Communicating between sender and receiver

Probably the most interesting Chromecast feature is the ability to establish bi-directional communication between the sender and receiver. Newscast sets up this communication pipeline entirely automatically. All you need to do is register the types of messages you'll be sending and receiving and attach callbacks to handle those messages.

Typically you'll want to attach these message handlers in the onReceiverCreated and onSenderCreated callbacks.For example:

onReceiverCreated = function(receiver) {
receiver.onMessage('ping', onCastReceiverPingMessage);


onSenderCreated = function(sender) {
sender.onMessage('pong', onCastSenderPongMessage);

Each message callback will be passed the message data. The message data must be a string. If you need to pass complex data you 'll need to handle serialiazing and deserializing that data yourself.

To send a message, simply use Sender.sendMessage() or Receiver.sendMessage():

er.sendMessage('ping', 'Hello receiver');

iver.sendMessage('pong', 'I see you sender');

See the messaging example for a working example of bi-directional communication.

Separate sender and receiver pages

Coming soon…

Gracefully reconnecting senders

Coming soon…

Development tasks

Grunt configuration is included for running common development tasks.

Javascript can be linted with jshint:

t jshint

Uniminified source can be regenerated with:

t concat

Minified source can be regenerated with:

t uglify

API documentation can be generated with:

t jsdoc

The release process is documented on the wiki.

License

Released under the MIT open source license. See LICENSE for details.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.