Gem Sidekiq provides an easy way to move some long and lengthy time consuming processes to be moved to background and make user experience better. Simply add gem "sidekiq" to Gemfile, run bundle install and create some worker with the long lengthy code.
Create a form, with action target as your back end controller and action where you want to handle. I will explain later why we need controller and action.
payment.html.erb
1234567891011121314151617181920212223
<%=form_tag('/custom_path')do|f|%><script src="https://checkout.stripe.com/checkout.js" class="stripe-button"//Publishable Key From stripedata-key="<%=STRIPE_PUBLISHABLE_KEY%>"//Appliation Namedata-name="My Appliation"//Payment amount(in cents)data-amount="10000"//Description for paymentdata-description="my custom description"//Text as will appear on payment button.data-label='Proceed with payment'//Users email, who is payingdata-email="<%=user.email%>"></script><%end%>
This will add a nice looking button to your we page.
Clicking on button will open up a popup
Stripe will handle everything from card validation to authorization over card. When user is successfully authenticated the form will be submitted and we get a token at backend alongwith other form fields value. That is where backend comes into play. The token from above can be used only once. We can use it for
charge.id will return us the unique transaction id for which we can save.
Directly charge the user
begin
charge = Stripe::Charge.create(
:amount => 1000, # amount in cents, again
:currency => "usd",
:card => token,
:description => "payinguser@example.com"
)
rescue Stripe::CardError => e
# The card has been declined
end
To check all the commands executed we can simply run history command. This will give us a nice long out put of the all commands executed via bash.
history
bundle install -j 4
gem install passenger
sudo service apache2 restart
passenger-install-apache2-module
sudo subl /etc/apache2/apache2.conf
sudo service apache2 restart
sudo a2ensite codesapling.conf
sudo service apache2 restart
rails s
But at critical times we might need to check the timestamps along with the commands executed on main server machine. To help that we can simply add a variable HISTTIMEFORMAT to bashrc
Web is built all around sending request to server and getting a response back from server, unless browser won’t request an update, the state will remain same. Although. AJAX saves a lot of pages reload and make the web feel more dynamic. But, still we need to make a lot of AJAX request. Which requires either some user interaction or periodic polling to load new data from the server.
This way it creates a hefty load of requests on server for undesired reasons.
WebSocket: Bringing life to Web
WebSocket is a protocol providing full-duplex communications channels over a single TCP connection. In layman terms, there is a active connection between server and client.
It is kind of a pipe open at both ends server and client. Both can send any data and would be received at another end.
Getting Started
web_socket = new WebSocket('ws://mywebsocketwebsite.com');
Call back events
Sending Messages.js
12345678910111213141516171819202122
// Send Messageweb_socket.send('Ping');// Sending file as Blobvarfile=document.querySelector('input[type="file"]').files[0];connection.send(file);// When the connection is establishedweb_socket.onopen=function(){web_socket.send('Ping');// Send the message 'Ping' to the server};// Log errorsweb_socket.onerror=function(error){console.log('WebSocket Error '+error);};// Calback, for messages from the serverweb_socket.onmessage=function(e){console.log('Server: '+e.data);};
On server side we can use Ruby, Node.js, Java, Ruby, Python, C++ and all other popular frameworks and languages. In my last post how we can implement in Rails. In next few posts I will show we can build up a complete web socket based environment for chat.
Getting started with websocket in rails pretty simple and quick.
Add Gemfile gem to your Gemfile and run the bundle command.
Run generator: rails g websocket_rails:install.
In your application.js do
application.js
application.js
12345
vardispatcher=newWebSocketRails('localhost:3000/websocket');dispatcher.bind('tasks.create_success',function(task){console.log('successfully created '+task.name);});
In controller from where ever the notification has to be sent
This is it, the minimal setup for using websocket is complete. Every time a task is created, a messages is sent to all over the active websocket connections. I have done console.log. After getting data we can process it any ways we want. Such as :
Private Pub is a Ruby gem which uses Faye to publish and subscribe to messages. For installing just add this to Gemfile of rails application and run Bundle install.
╰─$ rails new chat_private_pub/
╰─$ cd chat_private_pub/
╰─$ echo "2.1.1" >> .ruby-version (optional)
╰─$ echo "chat_private_pub" >> .ruby-gemset (optional)
╰─$ echo "gem 'private_pub'" >> Gemfile (adds gem private_pub to Gemfile)
╰─$ echo "gem 'thin'" >> Gemfile (adds gem thin to Gemfile)
╰─$ rails g scaffold Message content (just to create basic CRUD operations for messaging)
╰─$ rake db:create db:migrate
╰─$ rails g private_pub:install
╰─$ rails s
╰─$ rackup private_pub.ru -s thin -E production (in another tab)
This completes the basic installation. Last command generates two file, one of which is config/private_pub.yml. This stores the configuration for the connection with faye server. Next is add private_pub.js to application.js
The original intent while creating UUIDs was to create distributed systems to uniquely identify objects without significant central coordination. Saying which means, anyone can create UUIDs and use them to identify something. There is a very high probability the same key will never be unintentionally created by anyone else to identify some other kind of object. Objects labeled with UUIDs can therefore be later merge into a single database without needing to resolve key/identifier(ID) conflicts.
Rails 4 gives native support for the type UUID in Postgres. In the following post I will explain how we can enable and use the UUID your Rails code.