Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Valet: A Realtime Server for Rapid Prototyping


While developing the Robin platform at One Mighty Roar, we’ve found ourselves pushing realtime data between things like RFID kiosks, Bluetooth devices, PHP servers, and various AngularJS frontends. Along the way, we’ve built a simple but flexible server that uses Socket.io and Node.js to make passing around realtime data as simple as possible.

We’ve come to think of Valet as less of a server and more of a tool – once you’ve set it up once, you can use it for many concurrent projects without any customization. In this post, we’ll go over some of the aspects of Valet and how to use it. There’s more information on the Github repo, including detailed instructions on deploying Valet on various platforms.

Installing Valet

Valet simply accepts data and re-emits it to connected clients. A client could be some Javascript running in the browser, a server, an internet-connected piece of hardware – anything that can handle WebSocket connections.

To install Valet, run:

sudo npm install -g valet

To start your shiny new Valet server on

localhost:9200

*, run:

valet

*Note: all of the examples on this page use the default URL http://localhost:9200. If you deploy Valet on a PaaS like Amazon EC2 or Nodejitsu, you can generally drop the port from the base URL.

Event bubbling with socket namespaces

One of our goals when building Valet was to be able to instantly use it for new projects, without writing or deploying any new server-side code. If a single instance of Valet is going to be passing data between multiple projects, it is necessary to separate the connected clients by project. Socket.io can do this out-of-the-box, and it’s called “namespacing”.

Clients (frontends, devices, or even other servers) determine what Namespaces they want to join by connecting to a specific URL. Multiple namespaces can be joined by calling

connect

  multiple times (only one WebSocket connection will actually be used). For example, to join the

/temperature

  and

/humidity

  namespaces, your client would have code that looks something like:

temperature = io.connect('http://localhost:9200/temperature')
humidity = io.connect('http://localhost:9200/humidity')

Valet extends Socket.io’s namespaces in a useful way: if you use more complex namespaces, your events and data will “bubble up”. For example, data you send to a namespace like /buildings/123/rooms/456/temperature will be emitted on the following three namespaces:

/buildings/123/rooms/456/temperature
/buildings/123/temperature
/temperature

This means that you can listen to

/buildings/123/rooms/456/temperature

  to get events from Temperature sensors in room 456,

/buildings/123/temperature

  to get events from all of the temperature sensors in building 123, or

/temperature

  to get events from all of the temperature sensors, everywhere.

Sending data to Valet

You can pass data to Valet by either sending a POST request to your Valet server, or by emitting a socket event called “post”. For example, to send data from your terminal using CURL:

curl -X POST -H "Content-Type: application/json" -d '{"event":"reading","data":{"temp":83.3826}}' http://localhost:9200/buildings/123/rooms/456/temperature

To send the same data using a socket is a two step process – first, you must connect to your Valet server on the root namespace. Then, emit an event called

post

  containing the

event

 ,

namespace

 , and

data

  you would like to emit. For example:

temperature = io.connect('http://localhost:9200');
temperature.emit('post',{
    event:"reading",
    namespace:"/buildings/123/rooms/456/temperature",
    data:{temp:83.3826}
});

In either case, an event called

reading

  with data

{temp:83.3826}

  will be emitted on the three namespaces above.

Listening for data from Valet

Anything that can run Socket.io can be a client. Simply require Socket.io on your client, connect on whatever namespaces you like, and listen for events.

Here is an example client that listens for temperature readings. To demonstrate the namespace “event bubbling” behavior described above, this example listens for events on the

/buildings/123/temperature

  namespace, even though they will be sent to the server on the

/buildings/123/rooms/456/temperature

  namespace.


    
    

Wrapping up

Right now, we’ve got three AngularJS apps, a PHP backend, and a Raspberry Pi all passing data through a single Valet server running on a (free) micro instance on ec2. Setting up Valet in a new project generally takes

We wanted an always-on, realtime, easy-to-use realtime server, so we built Valet. Hopefully you’ve gotten a sense for how easy it is to start building things that can communicate in realtime.

Valet is extremely young and will continue to evolve as we find new and interesting applications for it. If you’ve got a feature you would like to see, or you come across a bug, feel free to submit an issue or pull request via Github.





Source link

The post Valet: A Realtime Server for Rapid Prototyping appeared first on Nexstair Technologies.



This post first appeared on Nexstair | Creative Website Design And Wordpress T, please read the originial post: here

Share the post

Valet: A Realtime Server for Rapid Prototyping

×

Subscribe to Nexstair | Creative Website Design And Wordpress T

Get updates delivered right to your inbox!

Thank you for your subscription

×