Duda I/O: Websocket Chat

There is not much to explain, Websocket is being used widely for realtime notifications over the web and Duda I/O supports websocket through a package. I have written a simple chat example at server side to demostrate how it can be used, the front-end part is a tweaked client where i just performed minor modifications:

The interest part is the service code side:

#include "webservice.h"
#include "packages/websocket/websocket.h"

DUDA_REGISTER("Duda I/O Examples", "WebSocket Chat");

void cb_on_message(duda_request_t *dr, ws_request_t *wr)
{
    websocket->broadcast(wr, wr->payload, wr->payload_len, WS_OPCODE_TEXT);
}

void cb_handshake(duda_request_t *dr)
{
    websocket->handshake(dr);
}

int duda_main()
{
    /* Load the websocket package */
    duda_load_package(websocket, "websocket");

    /*
     * Define a callback, on every websocket message received,
     * trigger cb_on_message.
     */
    websocket->set_callback(WS_ON_MESSAGE, cb_on_message);

    /* Associate a static URL with a callback */
    map->static_add("/handshake/", "cb_handshake");

    /* Initialize the broadcaster interface */
    websocket->broadcaster();

    return 0;
}

In duda_main() we initialize the web service, loading the websocket package and setting a callback function to invoke when a websocket message arrives. Then we map the URL path who wWebsocket handshake and finally we instruct the websocket package to launch the Broadcaster service, this last one is necessary if you want to send broadcast messages.

Getting started
If you want a simple steps to try this example do:

  • git clone git://git.monkey-project.com/dudac
  • git clone git://git.monkey-project.com/duda-examples
  • cd dudac/ && ./dudac -g
  • ./dudac -w /path/to/duda-examples/050_websocket_chat/

Now you can point your browser at http://localhost:2001/wschat/

Documentation

For more details about the Websocket package and its available methods, please refer to the Websocket API documentation

Duda I/O

Since a few months i have been working in a C web services framework called Duda I/O, this can be considered a child of Monkey Project. Duda I/O runs on top of Monkey and aims to expose a C friendly API for building fast and scalable web services. It’s totally open source under the LGPLv2.

 

Framework Components


DudaC: Formally Duda Client Manager, it is a helper for the development and easy deployment of web services. It takes care of the setup of the development environment cloning the respective stack components and building each one. It also allows to run a web service on fly just pointing to it source code.

Duda Plugin: This plugin is an extension for Monkey Web Server, it mainly wraps the Monkey API and expose a more friendly C API for building web services. This plugin also takes care to hide the complexity of the HTTP stack in terms of threading, balancing and asynchronous socket events.

HTTP Server: As mentioned earlier, the HTTP stack is powered by Monkey, a high performant and Open Source Web Server. Monkey is a HTTP/1.1 non-blocking web server implmented through a strategy of a number of fixed threads each one holding their own events queue. Its pretty scalable and can take the most of SMP systems. 

Web Services: Web Service is a software component built on top of Duda Plugin API which execute different instructions through a mapping of HTTP URL requests and callback functions. In technical terms is a shared library loaded by Duda on runtime.

 

Features


Non-blocking: The whole HTTP stack is based in the non-blocking model for sockets, this means that it works on top of asynchronous events. Each working thread can scale to thousands of active connections. Is good to mention that a non-blocking model will not reduce the computing time or delays caused by the blocking calls used in your web service.

Lightweight: For a normal web service running, the global size of the running components in memory can be around of 400KB. The memory used will depends of your web service implementation and packages loaded. The stack components as Duda and Monkey aims to be lightweight and optimize the resources used.

Service oriented: One of the main features of Duda, is that it allow to register multiple web services under the same HTTP instance, as well each service can be assigned to a different Virtual Host (a Virtual Host can hold multiple web services).

Each service can map static URLs to specific callback functions or use the Map interfaces provided by Duda, this last one is pretty much similar to REST and provides a very useful set of methods to handle each request resources such as: methods, parameters and body content.

 

API Objects


When building/running a web service, a set of C pseudo-objects are exported to perform the setup and define callbacks for certains events, as well many objects are helpers to build responses and minimize the effort for the developer. Some API Objects available are:

  • Console
  • Cookie
  • Event
  • Param
  • Response
  • Request
  • Session

Each object expose a set of methods, for more details about the available methods for each objects refer to the API documentation.

 

API Packages


Besides the built-in API Objects available, Duda supports a packages system which aims to load on demand external objects to extend the core API capabilities. Some packages available are the following:

  • Base64
  • JSON
  • SHA1
  • SQLite
  • Websocket

Packages are included in Duda per users demand, if you miss some specific package functionality let us know to consider it development and further inclusion.

If you want to know more about Duda please refer to the following links:

more news coming soon, if you want to stay tuned make sure to register in the new mailing list…