node package manager
Orgs are free. Discover, share, and reuse code in your team. Create a free org »

rewire.io

alt text

Light, fast, easy to use, and auto-reconnecting if the internet drops. perfect for poor connection/pubilc WIFI and mobile devices.

For this we use https://github.com/einaros/ws & https://github.com/jdarling/Object.observe

We wanted to make sure also that the code you write on the server is the same as the code you write in the client/browser.

alt text

The setup of REWIRE allows you to hide server side code.

For examples of REWIRE.io in use today visit http://computerstudio.eu/$tweet-dj-radio-app.php (Build you own Android/iPhone radio streaming appliction) and http://computerstudio.eu/-team-about-contact.php (A simple contact form).

Contents

API

c.REWIRE.js & s.REWIRE.js dont not need to be altered just upload them to your public_html and they will handle all of your apps and bridge the transports.

Connecting the server: This code can be found in n.example.js (start your app up with unix command node /location/n.example.js). This file points REWIRE to your severs main app script inside a folder you must create above public_html. The folders name is private_js as all javascript above the public_html directory is hidden from the public and protected.

//require('location of s.REWIRE.js')('alow a client',[socket numbers],[port numbers],'main server script'); 
 
require('/home/engine/public_html/s.REWIRE.js')('example.com','843',[4,5],[8004,8005],'s.example.js');

Connecting the client: This code can be found in example.php. This is the page users will visit to view your app. It points REWIRE to your main clientside app script.

var s={},S={},data={'nickname':nickname};
 
//connect('server url',[socket numbers],[port numbers],data,'main client script'); 
 
$.getScript('c.REWIRE.js',function(){connect('server.example.com',[4,5],[8004,8005],data,'c.example.js');});

Now that you're all connected you are able to write the same code in both your client C.app.js and your server S.app.js (and soon in your C.app.swf)...

Messaging using ws.send

All these methods of sending have a 'func' part so you can give the message a name (like socket.io). data parts can have as many key:values as you need.

Talk to self

s[0].WIRE('func',{data:'simple'});

SHOUT (func,data,TARGETS,ENCODE,RELAY)

Shout to all users including self

s[0].SHOUT('func',{simple:data});

Shout as a broadcast to all users

s[0].SHOUT('func',{simple:data},['!']);

Shout as a broadcast to all users in a room

s[0].SHOUT('func',{simple:data},['!','roomName']);

Shout as a broadcast to all users in a room relay from socket 0 to socket 4 (encoding param is needed for this)

s[0].SHOUT('func',{simple:data},['!','roomName'],'BIT',4);

Message Events using ws.onmessage

All custom events are listened to in the same way by adding them to the WHEN array and checking if they exist. if an event doesn't exist you get a nice console.dir showing everthing you missed just incase your sleepy.

s[0].WHEN['func']=function(_){console.dir(_);}

Don't pile up events, deleting them is easy!

delete s[0].WHEN['func'];

Rooms (Server side only)

We like the concept of every app acting like a chat, so now we can SHOUT to a room if we want.

Join a room

s[0].JOIN('room0');

Leave a room

s[0].LEAVE('room0');

Multiple Sockets

Sockets are an object s={}; So we can set up multiple that behave the same on both the client and the server.

s[0] s[1] s[2] ...ect

Each socket has its own wss server listening to its own port or you get EADDRINUSE (the error saying hey this port is alreay in use). REWIRE.io nubers each socket to make sure that sockets don't interfear with each other and that they load in order.

NOTE: For rooms and broadcasting only the first socket port is assigned a nickname. All others are called _port. This prevents getting the same message for each socket because each socket is otherwise seen as a client when broadcasting.

Connecting Vs Reconnecting

When the page is visited the server builds the client from a nickname in the address bar (this can easily be customised with extra vars or even getting stuff from a db).

Then if your internet goes down, drops, server dies or whatever (normally you would need to refresh the web page)

This way the client web page now trys to reconnect to the server every 2 seconds. when it reconnects the page remembers all user details, even what event listeners were set, and rejoins all the rooms the user was ocupying.

Flash transport fallback

Complete and stable.

Flash API

Must be the same experience as the current api. The functions that handle ws.onmessage and ws.send will probably be vey different but I wan't to style it in a way that the coder can just write apps the same as current client and browser code (all the same, nice and easy).

I'm looking at this http://www.rivellomultimediaconsulting.com/node-js-for-flash-and-flex-introduction/ at the moment to see if I can get flash to talk to the net tcp part of ws. Help apprecieated.

Goals

  • Flash API

Try Me Example

  1. upload c.REWIRE.js to your client site
  2. upload s.REWIRE.js to your server site
  3. n.example.js is started by node in ssh. In n.example.js edit your s.REWIRE.js location
  4. In n.example.js edit your policy file to accept your client site
  5. In n.example.js edit your socket number names
  6. In n.example.js edit your ports or open up ports 8004,8005 on your website
  7. In n.example.js name your js script for your main app code
  8. On your server in the directory above public_html make a folder called private_js and put your main s.app.js script there
  9. get https://github.com/jdarling/Object.observe (check re.php script tags put them where you like)
  10. npm install jquery
  11. npm install json-stringify-safe
  12. npm install try-json-parse
  13. npm install ws
  14. npm install policyfile
  15. In example.php point to your server
  16. In example.php number your sockets
  17. In example.php name your ports
  18. In example.php point to your client main app code, this goes inside public_html
  19. In your browser navigate to http://yourwebsite.com/example.php?n=your.name
  20. Upload c.app.js to your client public_html
  21. you may now write your own API events & commands in both c.app.js and s.app.js