Giter VIP home page Giter VIP logo

spacebrew.js's Introduction

Spacebrew Javascript Web Library and Core Examples

This repo contains the Spacebrew Library for Javascript along with documentation and example apps. This library was designed to work on front-end (browser) envrionments, and back-end (server) environments. Below is a brief overview about Spacbrew, followed by a short tutorial on how to use this library.

  • Current Version: 1.4.2
  • Latest Update: March 16, 2020
  • Main Contributors: Brett Renfer, Julio Terra, Quin Kennedy, Eric Ishii Eckhardt

Jump to:

About Spacebrew

Spacebrew is an open, dynamically re-routable software toolkit for choreographing interactive spaces. Or, in other words, a simple way to connect interactive things to one another. Every element you hook up to the system can subscribe to, and publish data feeds. Each data feed has a data type. There are three different data types supported by Spacebrew: boolean (true/false), number range (0-1023) or string (text). Once elements are set up, you can use a web based visual switchboard to connect or disconnect publishers and subscribers to each other.

Learn more about Spacebrew here.

Using Javascript Library

Before you get started you need to download the spacebrew library, and add it to your project's directory so that you can import it into your web app's code. To download the latest version of the library just click the "ZIP" button above.

1. Import javascript library into your project

Import the javascript library into your project using a script tag in the appropriate html page. We usually include the page's javascript code in a separate file as well. This file should be imported after the library.

<script src="path/sb-1.4.2.js"></script>
<script src="path/your_scripts.js"></script>

2. Create a Spacebrew object

In your_scripts.js file, the first thing you need to do is create new instance of a Spacebrew Client object using the Spacebrew.Client constructor and assign it to a variable or object attribute.

var sb = new Spacebrew.Client( server, name, description, options );

Constructor Parameters The constructor parameters, server, name, description, and options are all optional. The options parameter supports three options: port, an integer that specifies the port number of the spacebrew server, debug, a boolean value that turns on console messages when set to true, and reconnect, a boolean variable that turns off automatic reconnect when set to false. When running in a browser, the Spacebrew Client library will look for the server hostname, app name, app description, and server port setting in the query string; using server, name, description, port as keys.

If server is not specified in the query string or constructor, then the app will attempt to connect to the public cloud server at sandbox.spacebrew.cc. The name will default to the app's full URL, if no name is provided via the query string or constructor. The description will remain blank if no description is specified. The port defaults to 9000 if not specified in the constructor or query string.

3. Configure Base Name and Description Feeds

You can also set the base app name and description using the Spacebrew.Client library's name() and description() methods. The app's name and description must be set before the Spacebrew connection is established.

sb.name("myApp")
sb.description("this is a spacebrew app.")

4. Configure Data Subscription and Publication Feeds

The next step is adding the subscription and publication data feeds. Each one needs to be labelled and assigned an appropriate data type - "string", "range", "boolean" (custom data types are also supported, the names for these data types are arbitrary, as long as the publisher and subscriber feature the same name). You can also optionally define a default value for any publication feed.

sb.addPublish( name, type, default );
sb.addSubscribe( name, type );

5. Define lifecycle and message event handler methods

Spacebrew offers lifecycle event hooks for connection open and close events - onOpen, and onClose; and for incoming message events of each data type - onStringMessage, onRangeMessage, of onBooleanMessage. You need to define the message handler methods in order to capture data from your subcriptions data feeds.

sb.onStringMessage = function onString( name, value ) {};
sb.onRangeMessage = function onRange( name, value ) {};
sb.onBooleanMessage = function onBoolean( name, value ) {};
sb.onCustomMessage = function onBoolean( name, value, type ) {};
sb.onOpen = function onOpen() {};
sb.onClose = function onClose() {};

6. Connect to Spacebrew

Now that you have configured the Spacebrew object it is time to connect to the Spacebrew server.

sb.connect();

7. Send messages

The send method enables you to publish messages via one of the publication data feeds. It accepts three mandatory parameters, a channel name, a data type, and a value. The value needs to correspond to the data type, otherwise the message will be ignored by the server.

sb.send( name, type, value )

Javascript Library Examples

Here is a list of the core examples that are included in this repo. These examples were designed to help you get started building web apps that connect to other applications, objects and spaces via Spacebrew.

Button (Boolean Example)

Web app with a button that publishes a boolean value every time the button is clicked. It also subscribes to boolean messages, which change the web app's background color.

Slider (Range Example)

Web app that subscribes and publishes range values and features three sliders that are used to send and display range values to and from Spacebrew.

String Example

Web app with a text box publishes text messages to Spacebrew. App also subscribes to string messages, which are displayed underneath the text box.

Dice (Custom Data Type Example)

Web app that publishes and subscribes to the custom data type called dice. It features a button that generates a random number between 1 and 6.

Spacebrew XY Pad (Range)

A simple canvas-based touch pad that sends two ranges–one for an X value and one for a Y. Very useful on mobile, where you may shake your phone to send a "clear" boolean.

Spacebrew Shared Canvas

An output example that takes in a set of messages to create a shared drawing canvas. Possible messages include an X and Y range for a virtual cursor; red, green, and blue ranges to adjust the background color; and a 'clear' boolean to erase the drawing.

Spacebrew Admin Mix-in

You can also integrate admin functionality directly into yor spacebrew client applications using the Spacebrew admin library along with the standard javascript mix-in. Please note that this mix-in is still in early development phases, which means that it will change and evolve a lot over the coming months. we will document the process for adding admin functionality into your client apps in the coming weeks.

Spacebrew Admin Mix-in Version Details

Current Version: 0.1.5 Latest Update: March 19, 2016
Main Contributors: Julio Terra, Brett Renfer

Using Spacebrew Admin Mix-in

Before you get started you need to download the spacebrew admin mix-in, and add it to your project's directory so that you can import it into your web app's code. This mix-in is included in the same folder as the Spacebrew javascript library.

1. Import javascript mix-in into your project

Import the javascript library into your project using a script tag in the appropriate html page. You should import this file after the Spacebrew library.

<script src="path/sb-1.4.2.js"></script>
<script src="path/sb-admin-0.1.5.js"></script>

2. Extend Spacebrew object with admin mix-in

After you create the Spacebrew object you need to extend it with the admin mix-in by calling the extend() method as illustrated below.

sb = new Spacebrew.Client();
sb.extend(Spacebrew.Admin);

3. Define admin event handler methods

Several event hooks are provided by the Spacebrew admin mix-in. Please refer to the source file for more details regarding the arguments associated to each hook.

// triggered when new client connects to server.  
sb.onNewClient = function( client ) {};

// triggered when existing client is reconfigured.  
sb.onUpdateClient = function( client ) {};

// triggered when an existing client disconnects from server.  
sb.onRemoveClient = function ( name, address) {};

// triggered when a route is added or removed.  
sb.onUpdateRoute = function ( action, pub, sub )

4. Add and remove routes

The addRoute and removeRoute methods enable you to add and remove routes between any client apps that are connected to the Spacebrew server. These methods require the following information for publisher and subscriber associated to each route: client name, address, pub or sub name. You can provide this information as individual parameters, or group them together into separate publisher and subscriber parameter objects.

sb.addRoute( publisher, subscriber )
sb.addRoute( pub_client, pub_address, pub_name, sub_client, sub_address, sub_name )
sb.removeRoute( publisher, subscriber )
sb.removeRoute ( pub_client, pub_address, pub_name, sub_client, sub_address, sub_name )

The four methods below enable you to easily create and remove routes that involve the client app with the embedded admin functionality.

sb.addSubRoute( pub_name, sub_client, sub_address, sub_name )
sb.addPubRoute( sub_name, pub_client, pub_address, pub_name )
sb.removeSubRoute( pub_name, sub_client, sub_address, sub_name )
sb.removePubRoute( sub_name, pub_client, pub_address, pub_name )

License

The MIT License (MIT)
Copyright © 2012 LAB at Rockwell Group, http://www.rockwellgroup.com/lab

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

spacebrew.js's People

Contributors

robotconscience avatar julioterra avatar quinkennedy avatar adenine avatar snorpey avatar nbartzokas avatar murilopolese avatar

Stargazers

Felix Groll avatar Benjamin Bojko avatar Serhii avatar  avatar  avatar Jordan Shaw avatar Jeremy Sarda avatar Jiim Knopf avatar Matthew Gotth-Olsen avatar Spencer Mathews avatar Bogdan Pestritu avatar Ben Li avatar Zhuoxi Song avatar Simon Schweißinger avatar Clint Bettiga avatar Sean Barclay avatar Michael Anthony avatar Derek Rushforth avatar roadlabs avatar Nick Porcino avatar Naoto Yoshimoto avatar Danqing Wang avatar Bryan avatar Brian Williford avatar  avatar Adarsh avatar Martin Bravo avatar  avatar Ashley Marie Quinn avatar Mike Heavers avatar  avatar Harry Chen avatar Randall A. Gordon avatar Robert C Edwards avatar

Watchers

Eric Ishii Eckhardt avatar  avatar  avatar Sean Barclay avatar Emmanuel Flores Elias avatar Lars Berg avatar roadlabs avatar  avatar Nick Porcino avatar  avatar James Cloos avatar  avatar Michael Anthony avatar  avatar james tichenor avatar  avatar Gustavo Faria avatar Bogdan Pestritu avatar

spacebrew.js's Issues

Send interval needs to be rethought

Right now just leads to message send failure without notice.

Maybe it could be tied to a route name instead of everything? For example, right now even if I set send interval to 0, this code fails:

sb.send("r",red);
sb.send("g",green);
sb.send("b",blue);

(green isn't sent). I understand the point of the interval, but it needs some more detail to be useful and not confusing. @julioterra what do you think? Should we make it an option a la reconnect?

Issues on Safari - Undefined Function on line 82

Brett, I trie to run a spacebrew app that used the js spacebrew library that you developed and I encountered a two issues. Here is the second of the two:

Line 81 - CODE: "this.socket.onopen = this._onOpen.bind(this);" ;
Line 81 - ERROR: "'undefined' is not a function (evaluating 'this._onOpen.bind(this)')"

This issue is coming up whenever the "bind" method is used. The reason is, many versions of safari don't support "bind". I am trying to upgrade the version of Safari on my computer to the latest, which supposedly does support "bind".

Not sure if this issue is worth fixing, or if we should just notify users that it doesn't run on older versions of Safari.

Spacebrew.js doesn't work in node

Beside checking for node environment, module exports and using wc node module the spacebrew.js 1.3.0 doesn't work in node.

#140 - you use window without checking if it's defined (it's not in node)
this.debug = (window.getQueryString('debug') === "true" ? true : (options.debug || false));

#226 - you use browser style this.socket.onopen handlers while ws module requires this.socket.on('open'...

I fixed all that successfully published client on the cloud and then failed because of SyntaxError: Unexpected token u error in incoming message.

Are there plans to officially support node / Plask apps?

Switch to "listener" scheme instead of override for on...Message

Up for discussion, but I think we should switch to the scheme we used in the past to add listeners to functions instead of just overriding onBooleanMessage, etc:

// now
sb.onBooleanMessage = myBooleanFunction;

// future
sb.onBooleanMessage( myBooleanFunction );

// OR
sb.addListener( sb.onBooleanMessage, myBooleanFunction );

Issues on Safari - TypeError on line 71

Brett, I trie to run a spacebrew app that used the js spacebrew library that you developed and I encountered a two issues. Here is the first of the two:

Line 71 - CODE: "this.isConnected = this._isConnected = false;
Line 71 - ERROR: "TypeError: setting a property that has only a getter."

I noticed on your code that you only set a getter function for the this.isConnected property. To resolve this error I took out this.isConnected from this line of code as it did not seem like it was performing any important function - though I may be wrong.

Remove Jquery where possible

We're just being lazy in a lot of places!

window.onload, "onmousemove" events, etc are better than including a whole other library. Totally fine keeping the jquery UI stuff in, though.

Add constants for built-in types

To free us up for possibly changing the underlying names in the future.

// these could also be in all caps...
Spacebrew.Boolean = "boolean";
Spacebrew.Range = "range";
Spacebrew.String = "string";

// ex
sb.addPublish( "amChilling", Spacebrew.Boolean, true );

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.