Giter Club home page Giter Club logo

background's Introduction

Background

A web application that does a “pretend email sending” in the background. The background process then pings back to the client when the action is done (essentially a sleep for some seconds).

Uses resque, redis, faye to accomplish this.

Installation

  1. Install redis

  2. Do a bundle install to install gems resque, faye. Do a db set up (just creates a users table)

    bundle install rake db:migrate

  3. Run the rake job to start listening to workers (resque side) - these workers listen to requests coming from the rails web app via user browser requests.

RAILS_ENV=development rake resque:work QUEUE='*'
  1. Start the faye server

rackup faye.ru -s thin -E development

  1. Start the web server

rails s

  1. For checking the resque job queue - go to

http://localhost:3000/resque/queues
  1. Create a new user by going to localhost:3000/users and clicking on New User link. To submit an email job for the user with user id 1

Go to http://localhost:3000/users/1/ and click on the Email link

You should get an alert from the server side in a few seconds. That is it - you have
exercised the power of faye and redis to submit a job and get a message back
from the server to the client asynchronously`.

When you click on the email link, following events happen:

. In the email action of users controller, we submit a job to Emailer worker . The worker (app/workers/emailer.rb) picks the job in which it sleeps for a few seconds and then sends the “email sent” message to the faye server on the channel “/messages/1”. In this example, the channel id is simply the user id but in real application, it would also have a random guid component so that we do not confuse between different messages and all. . On the client, we are already listening on this channel - so we get the message and give an alert - following is the logic in layout/application.html.erb that does this

<script>

$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
  var subscription = faye.subscribe("/messages/1", function(data) {
    alert(data);
  });
});

</script>

  1. As mentioned, in real application, you would create a separate channel (includes a random guid) for each job - so the clients do not interfere with each other. You would also make the worker more light weight by not loading the entire rails environment for each worker task.

background's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.