samedi 31 janvier 2015

Server-side plugin mechanism for Rails application


I have a Rails application that has the following attributes:


The application



  • can have a number of clients (web-browsers)

  • can run in different modes/programmes

  • can receive simple events (e.g. when a new client connects)

  • can output primitive information (for now just text)


So is one examples of what I mean by modes:


Mode 1: Whenever a new client is detected the new client should output "Hello Anyone" and all existing clients should output "Welcome!"


Mode 2: Whenever a new client is detected the new client gets assigned a random number and he "says" my number is "5". The other clients say "My number is still "XYZ".


Now this seems to be fairly simple and I have the whole client part (using Ajax polling) as well as the server part set up. Everything runs smoothly so far.


However, what I want to do now is extracting the mode-logic out into a pluggable architecture using javascript.


So the rough approach would be:



  1. The application is in a given mode

  2. An event happens

  3. The application processes the mode's javascript code with the event

  4. The application outputs any results and sends them to the clients (polling at the moment)


Since the whole stuff is event-driven I think javascript/coffeescript is a very good candidate for this. So the mode could be defined like so:



# Coffee
onClientConnect (stage, newClient)->
stage.sendToExistingClients('Welcome!')
newClient.send('Hello Anyone')


The variables stage and newClient are now some part of my JS API which are hooks for the mode to connect to. Also the onClientConnect method.


How can I execute this Javascript-based code on my server, provide the necessary hooks and process the events so that all the clients display their respective output?





Aucun commentaire:

Enregistrer un commentaire