Valet: A Realtime Server for Rapid Prototyping

A simple server built on Node.js and Socket.io. You can deploy it locally with one command, or remotely with a few clicks. Clever namespaces allow you to use Valet for multiple projects simultaneously.

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:

To start your shiny new Valet server on localhost:9200*, run:

*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:

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:

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:

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:

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 < 10 lines of code on the client-side, and no work at all on the server-side.

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.

Posted Thursday, August 22nd, 2013 · Back to Top

SPONSOR

Add Comment

7 Comments 1 Mentions

  1. Arunkumar Gudelli Author Editor

    Thanks for sharing.. You saved my Time..

    ·

  2. خرید ملک در آلانیا Author Editor

    exactly what i am looking for this…cool work…great!!!!

    ·

  3. Naba Now Author Editor

    This is Really amazing Post also Great Blog. Thank you very much for Sharing This Post. Keep up it.

    ·

  4. تور ترکیه Author Editor

    It was a very useful article.
    thanks

    ·

  5. سانترال Author Editor

    i can use all of the article in my job tank you for all tings

    ·

  6. GraphicIded Author Editor

    This is Really amazing Post .Great Blog Thank you

    ·

  7. طراحی سایت Author Editor

    tank you for sharing this post its good

    ·

 

Build Internet by One Mighty Roar. Since 2008.