Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Sunday, February 3, 2013

Apple Push Notification Service in Rails

Push Notifications Overview

Getting push to work for your app takes quite a bit of effort. This is a puzzle with many pieces. Here is an overview:


  1. An app enables push notifications. The user has to confirm that he wishes to receive these notifications.
  2. The app receives a “device token”. You can think of the device token as the address that push notifications will be sent to.
  3. The app sends the device token to your server.
  4. When something of interest to your app happens, the server sends a push notification to the Apple Push Notification Service, or APNS for short.
  5. APNS sends the push notification to the user’s device.

Converting and Adding Certificate

Once you have the certificate from Apple for your application, export your key and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:

  1. Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
  2. Right click and choose `Export 2 items.
  3. Choose the p12 format from the drop down and name it cert.p12. Now convert the p12 file to a pem file:
$ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts

If you are using a development certificate, then change the name to apple_push_notification_development.pem instead. Add the .pem file in config/ directory of the Rails application.

Installing Gem for Push Notifications

Gem to use for Apple push notifications is “apn_on_rails” Github Link
To add the gem to your Rails app just add this line to your Gemfile.

'gem apn_on_rails'

or Type in this command at terminal

$ sudo gem install apn_on_rails

Registering your App for Apple Push Notifications

Before adding push notification service to your Rails app, you have to register your app for the Push Notification Service with the certificate created earlier.

Register your App with credentials as (Ruby Code)


app = APN::App.new
app.apn_dev_cert = File.read('config/apple_push_notification_development.pem')
app.save

The code creates a new APN::App, adds a Developer certificate and saves it to the database.

Registering the devices of the users to your Rails Application

After registering the app for push notifications, The next step is to register the devices of all the users of your Application. Registering the user devices to the service is through there “device tokens”. When the user registers to the app in the “client side”, the device token is retrieved and sent to the “Rails App” in the POST request.
Use that device tokens to register the users for the Apple Push Notification Service.

Register the users to the service as (Ruby code)


device = APN::Device.new
device.token = self.device_token
device.app_id=app.id
device.save


The code creates a new App Device Instance. Adds the device token of the user fetched from the client app. Adds an app_id to the device with the id of your App created earlier and then save the device to the database. Now your user is ready to accept the Push Notification from the server.

Single Notifications

If you want to send individual notifications.


notification = APN::Notification.new
notification.device = device
notification.badge = 5
notification.sound = true
notification.alert = "Message by Admin"
notification.save


The code creates a Notification Instance. Adds a device you want to send notifications too. Adds badge, sound and alert message to the notification and save it to the database.
Then the send the notification to the device using


 APN::App.send_notifications

Group Notifications

The gem does has support for group notificatiosn too. Sending messages to many devices at the same time.

Get all the registered devices using


devices = APN::Device.all

Get the App registered for the service


app = APN::App.first


For sending group notifications,we have to create a group and add the devices array to the group.

Create a group and add name and app_id and save it to the database


  group = APN::Group.new
  group.name = 'products'
  group.app_id = app.id
  group.save

Adding the devices array to the group


group.devices = devices

Adding a Group Notification

Create a Group Notification instance. Add the group instance to the notification group. Add alert, badge messages and save it to the database


notification = APN::GroupNotification.new
notification.group = group
notification.alert = 'Event got locked'
notification.badge = 4
notification.sound = true
notification.save


Then send the Group notification to all the devices as


 APN::App.send_group_notifications
APN::App.process_devices


This sends the message to all the devices registered.

Saturday, May 19, 2012

Idea : Video/Audio Compression on Fly for Streaming


Problems :
  •  Streaming videos/audios from the web is a problem in low bandwidth places.
  • Streaming is not smooth.
  • The Video is delivered in one standard resolution for streaming which is high to stream smoothly in Low bandwidth places.
  • Why serve users with High resolution videos when they can't stream it ?
  • Stream something of low resolution videos/audios which users can stream rather than nothing.
The Problems faced made me to come with an Idea, which can help solve this problem of streaming.

Idea :
  • Detect the bandwidth of the users streaming from your website on the fly.
  • Based on the bandwidth used by the user provide him with video/audio appropriately compressed for smoother streaming.
  • For Low bandwidth users, Compress the high resolution video/audio on fly and stream the data to them.
  • For High Bandwidth users provide them with Good resolution videos/audios.
  • An algorithm which compresses high resolution videos on fly and stream it to them can be implemented at server side.
You tube has implemented this Idea, wherein they detect the bandwidth of user and switch between 1080,720, 480,360 and 240px videos. But this Idea is not implemented by Video/audio streaming websites.

Readers do comment on the Idea and your views for the same.

If you could please take a minute to answer these questions :
  1. Is this Idea being implemented by any Streaming websites ? Name them ?
  2. Is there any specific algorithm which compresses videos/audios on fly?
  3. Can the Bandwidth detection of the users of the website be detected with accuracy?
 Thanks for taking time in reading this article and answering the questions.





Sunday, May 6, 2012

SPDY : Let's Make the Web Faster


Here is a brief description of what SPDY is ?

SPDY is an Experimental Application layer protocol which augments HTTP protocol with new features and best practices. HTTP and TCP are the standard protocols used for the web today. There is not much we can optimize at the transport level (TCP) so the HTTP protocol at the application layer was rethought. HTTP protocol at the application layer has a lot of drawbacks. Thus SPDY was designed to overcome these drawbacks of HTTP and help load the web Pages faster. 
SPDY introduces some of features such as Multiplexing of Streams, Request Prioritization, Server initiated streams and HTTP Header Compression. It has been experimentally determined that page loads are 50% faster in SPDY than in HTTP. There are both client and server level support for SPDY protocol. SPDY is easier to deploy and is also server efficient. SPDY with SSL is also implemented for secure transmission of data over SPDY protocol.
 Important goals of SPDY include faster page loads and improved web security. SPDY also supports Bidirectional streams. Most of the client end Browsers such as Firefox, Silk and Chrome now fully support SPDY protocol. At the server side, Servers such as Apache, Jetty etc. are now supporting SPDY. Most of Google Web sites now have completely implemented SPDY protocol.


Sunday, January 1, 2012

Exploring Things ......

First and Foremost " Happy New Year 2012".
Recently I have been exploring a lot of things which demands an article completely dedicated to it.

Mozilla Web QA Team :




Recently I started contributing to the Mozilla Web QA (Quality Assurance ) Team. Thanks to Matt Brandt for his support and motivation. I worked on two projects for Mozilla , the affiliates and the mozillians projects. As a part of a QA team we conducted various Exploratory , Manual and automated tests on these websites. The main focus of the projects for the release was the Browser-ID authentication system. We conducted a lot of tests on the new authentication system (Browser-ID) for the websites affiliates and mozillians. In this process I did make some good friends working at Mozilla. Matt Brandt (QA Lead on Input, Breakpad, Affiliates, and Mozilliansbeing my mentor for all my ventures , Stephen Donner (WebQA Manager for Mozilla ) for motivating me through the process. I even did had a nice  chat with developers of mozillians and affiliates on IRC channels #affiliates and #mozillians.
The Most important thing that drove me towards contributing to the Mozilla projects was the transparency with which they operate all there activities which include development ,testing or release of the products. All of there projects are Opensource hosted on github.
I did learn a lot of stuff during this venture. 
  • I started coding in Python and writing automated test cases for testing websites. 
  • Learnt the working of Selenium server and use of it for automating test cases.
  • Understood the complete life-cycle of a bug.
Python :

Motivation behind learning and coding in Python mainly came from Mozilla . Mozilla's most of their projects require Python knowledge and since I started contributing to the Mozilla Team , I needed Python for writing unit and automated test cases for testing websites. I learnt most of the stuff about Python from a book "Dive into Python" and Markana tv videos on Python.
Python is indeed a great language and it was always my dream to code in Python some day but did not get enough motivation and time .
Github repo for Python code snippets https://github.com/rShetty/pythology.

Javascript :

Motivation behind learning Javascript was the project for my 7th sem (The QA Portal). To bring dynamicity and to implement events to the site we needed Javascript . I started my Journey to learn JavaScript with book "JavaScript: The definitive guide " One of the finest books of all times for JavaScript. Further I explored the Jquery Library in JavaScript which is just Mind Boggling Library.I also coded in node.js  (Javascript platform for building fast ,scalable applications ).
Github repo for the Javscript https://github.com/rShetty/jsbits.

So far it was great experience exploring stuff . Hope in 2012 I explore more awesome stuff and Primarily code more :)








Wednesday, December 21, 2011

node.js for Beginners





In this article we will be discussing the very basics of node.js with some sample code snippets and examples.

Introduction :

node.js is a platform built on chrome's v8 Javascript runtime for building scalable, secure and fast network applications.It was created by Ryan Dahl.
It is a server side javascript .It is evented , Non Blocking I/O system.It is specialised in concurrency control.

You can Download the package from here http://nodejs.org 

Basic Architecture :
  • The System uses event loop and does not exit until there is no other work to be done.
  • It is Non Blocked system meaning that the system is capable of asynchronous communication (means can start handling other request without waiting for the response of the previous one).
  • In other systems usually a thread is created for each request to the server. Since this thread based model has serious disadvantages like this ,thus this architectural design is dropped in node.js Basically it allocates heap for each connection. 

Hello World Program in  node.js

 var http = require('http');
 var conn = http.createServer(function(request,response) {
  response.writeHead(200,{'Content-Type':'text/plain'});
  response.end('Hello World\n');
 });
 conn.listen(8000);
 console.log('Server running at http://127.0.0.1:8000/');
Note :Semicolons at the end of each statement is optional.

Explanation : In the above program we are creating a simple web server using node.js .In the program above we are including http module in the first line.
Then we are creating a server using createServer API which takes a callback function.So,every time a connection is made to the server by the client this callback function is called which writes headers to the client and ends the connection by writing Hello world (usually the body).The program is listening on port 8000.

Run : After you have downloaded and installed node.js on your system. Save the file with the above code snippet as helloworld.js .Then in terminal type

# node helloworld.js 

This starts the server on port 8000 in localhost.

So open another terminal and telnet to the server as

# telnet localhost 8000

Hello world gets printed

In Browser....

http://localhost:8000

Building large scale network applications is simple and robust using node.js

I have implemented the echo server using node.js .You can find the code here 

A simple chat server is also implemented . Can find the code here

All my Javascript code snippets with node.js examples and illustrations are available at
https://github.com/rShetty/jsbits

You can get the node package manager for installing various modules from here http://www.npmjs.org/

Express node.js is a popular web application framework for node.js 
http://www.expressjs.com/ 

For any doubts or clarifications of the code snippets above do leave a comment or send me mail at coder.rshetty@gmail.com







 








Sunday, June 12, 2011

Google Pac-Man Doodle


Pac Man Doodle


Friday, June 10, 2011

Google Guitar Doodle





Lester William Pollsfus was an American jazz and country guitarist , songwriter and inventor . He was Born on June 9th 1915 . The search Engine giant Google celebrated the 95th Birthday of the legendary guitarist Les Paul . The Google Doodle on 9th of June looked as shown above which is a design of Guitar . The Guitar can be played and the tune can even be recorded . The Doodle used HTML 5 and Javascript primarily for Development  .


Tuesday, June 7, 2011

Firefox Hack


Here is a simple Firefox hack to introduce backward and forward compatibility into your Firefox Browser.



Friday, June 3, 2011

Friday, April 22, 2011

404 Error

404 Error is the most famous error known among the Web masters. This article is dedicated to my experience on Google search Engine during which I came across this error.



Tuesday, March 1, 2011

Top 10 Chrome Extensions

Google Chrome is one of the most robust, fast and secure browser of recent times. It was developed by Google through Chromium project. Chrome browser comes with a lot of  cool extensions . Chrome already has more than 1000 extensions just next to Mozilla Firefox leading at around 1300 extensions.
Extensions for Chrome browser can be found at https://chrome.google.com/extensions/

Some of my favorite extensions in Chrome being :


Monday, August 23, 2010

EPIC BROWSER : BROWSING THE INDIAN WAY

epic_browser 
Hidden Reflex , an Indian start-up company in Bangalore has come out with a Browser named EPIC. This is the first Indian Browser developed completely by Indian Engineering Brains in Bangalore which was released on July 16 2010.
Based on the source Mozilla platform , this new browser claims to be faster ,safer and easier to browse . But How much of it is true still remains a Myth.
Let’s break the Myth and explore the features of this Indian Browser.
Can be downloaded from www.epicbrowser.com



Sunday, August 8, 2010

MOZILLA FIREFOX : BROWSER OF THE FUTURE


Mozilla as we know is a global community of thousands of people all over the world who believe in the power of technology in enriching people's lives . It is a non-profit organization .
Mozilla Firefox being the open-source software project , It's code is readily available to the public on internet and other media.
Mozilla Firefox is a browser developed by Mozilla corporation which is presently working on Firefox 4.0 beta which can be downloaded at http://www.mozilla.com/en-US/firefox/beta/
Presently the stable version of firefox being the Firefox 3.6.8 , which is readily available for download at http://www.mozilla.com/en-US/
Mozilla Firefox is the most secure, robust , and fully customizable web-browser.
It is indeed one of the best browsers in the market with its features such as :