AngularJS-large

AngularJS vs BackboneJS vs EmberJS? What I decided and why?

I was researching for Javascript frameworks/libraries to use in my next project. I found three promising JS frameworks.

  • Angular.js
  • Backbone.js
  • Ember.js

What I was looking for that framework should be easy to learn and should be integrated in the project easily. Keeping in that mind I searched a little bit about each and went through some reviews. What I concluded I’ll just say it in one line for each.

Backbone.js – You need to write lots of boilerplate code to get to working.

Ember.js – It has a steep learning curve.

Anguler.js – Just in between. Offers more than backbone and easier to learn than ember.

So, that was it! I picked Angular for my next project since it was easier to learn and you don’t have to do a lot to get it working in your project. Since, the project was new and deadline was short so this was the requirement. So, I decided to go with Angular and I’m glad I made the right decision.

What’s your call? What you picked and why? I would like to listen in comments.

windows10_windows_product_family_9-30-event-100464966-orig

Microsoft Windows 10 Great UI Changes

My first experience with Windows 8 inspired bafflement and frustration. But I walked away from my first few minutes with Windows 10 with a sense of jealousy. It looks like a significant improvement, and I want it on my PC right now.

Microsoft is launching Windows 10 as the new face of both Windows and eventually Windows Phone. At one point during Microsoft’s Tuesday press event, Terry Myerson, the executive vice president in charge of Microsoft’s OS group, called the new OS “our most open, collaborative OS project ever.” Collaborative, indeed. Microsoft is looking for user feedback, and what I demo’d on Tuesday may not be the same OS that customers receive next year.

Microsoft executives didn’t even characterize the system as an alpha; they referred to it as a “build.” So with Windows 10 tentatively scheduled to be launched around the middle of 2015, there’s quite a bit of time to change, remove or add features before the system launches.

windows10 tech preview start menuIMAGE: MICROSOFT

The Windows 10 Start Menu fues Windows 7 icons and Windows 8 Live Tiles.

That said, we can still point to various features that embody the new Windows 10 experience, and will almost certainly make the cut. These include the revamped Start menu; the new “task view,” virtual desktops and ALT-TAB features; windowed apps; and the new “snap assist” capability. Granted, I had a just a few moments to play around with each. But I quite liked what I saw, and if you sign up for the new Windows Insider program, you’ll have a chance to form your own impressions beginning on Wednesday.

The revamped Start Menu: clean, intuitive

I’m not wholeheartedly in love with the new Start Menu. Aesthetically, it looks like someone surgically conjoined the Windows 7 and Windows 8 experience. Move past that inelegance, however, and it’s darn useful. On the left, there’s a list of frequently used apps, along with shortcuts to PC settings, as well as your documents and pictures folders. At the bottom, there’s a shortcut to launch an “all apps” view.

On the right, the Live Tiles reproduce the functionality of the Windows 8 Start screen, with resizeable tiles that can dynamically show you how much unread mail is left. It appears that you should think of Live Tiles more like notifications rather than app shortcuts, although you can use them either way. Microsoft’s demo station had a large oversize tile showing the current calendar appointment, which seemed appropriate.

windows 10 start menu longMARK HACHMAN

If you want, you can resize the Start menu, increasing or decreasing its size and adjusting its position.

Also, if you want to resize the Start menu itself, you’re free to do so.

As some leaked videos foreshadowed, you can revert to the Windows 8 Start page, if you so choose. But that option wasn’t checked off, leading me to believe that most people would prefer the desktop experience.

A new Windows world: Task view, virtual desktops, and ALT-TAB

One of the Microsoft executives I talked to referred to the new “task view” as a “poor man’s multimonitor setup.” I can understand why.

Virtually all Windows users use ALT-TAB to quickly shuffle between windows. It’s a great way to move from one task to the next. That capability is still available in Windows 10.

windows 10 alt tabMARK HACHMAN

Using Alt-TAB to cycle through windows isn’t that different than Windows 8, but you can see more of what each window holds.

What’s different, however, is the new “task view” button. Down in the Windows 10 taskbar, third from the left, is a button that, when clicked, brings up an array of “virtual desktops.”

What’s a virtual desktop? Think of it as a virtual display.

If you’re running a multimonitor set-up, chances are you’re already allocating different applications to different screens: a browser window on one display, Outlook on another, and maybe a chat app on your docked notebook. But with Windows 10, Microsoft allows you to snap more than one app to a screen. So if you choose, you could fill a secondary monitor with an Outlook pane and a PowerPoint file that you’re referencing in an email to the colleague.

windows 10 taskbarMARK HACHMAN

The Windows 10 taskbar includes the Windows button, which launches the Start menu, the new Task View button, and the Search button. To the far right, the “underlined” apps show that they’re located inside a virtual desktop.

But if you have one monitor, tapping the task view button—or more usefully, Windows+TAB—swaps between desktops, which are displayed on the bottom of the display. So if you have a “project screen” with PowerPoint, a browser window, and OneNote all contained within it, you can swap to an entirely different virtual desktop, or workspace, perhaps with Facebook and Xbox Music. You’ll also notice the apps themselves are shown above the desktops themselves, so if you can’t remember what virtual desktop actually owned that app, you can just jump into it regardless.

There’s also a feature that may or may not make it to final release: On your taskbar, you’re probably used to instances of multiple browser windows stacked on one another. But in Windows 10, you may also see that app “underlined” by a horizontal bar, showing that it’s in a virtual desktop.

windows10 tech preview virtual desktopIMAGE: MICROSOFT

The new task view shows you your virtual desktops, and the apps contained within them.

For now, virtual desktops are a convenience, but they could also emerge as a security feature, allowing users to assign permissions to different ones. Microsoft officials wouldn’t tell me if they’ll be isolated from one another or “sandboxed” over time.

Snap snaps with Snap Assist and windowed apps

And what about Snap, the nifty little feature that fills half of a Windows 8 screen? That’s been improved as well. Every app in Windows 10 can be dynamically resized in a window, although it remains to be see how well this works in practice.

Windows 10 snap assistMARK HACHMAN

Snap a document to one side of the screen, and Snap Assist will suggest some others.

In Windows 8, apps can be snapped by clicking the Windows key and the left or right arrow, snapping them to the left or right of the screen. That fills half the screen. With Windows 10, up to four apps can be snapped per screen, maximizing your productivity.

What’s neat, though, is that once you snap an app, Windows 10 suggests another, similar app that you might want to snap next to it, from a small collection of windows. The feature is intended to save you the hassle of hunting about through menus to actually construct a virtual desktop. Time will tell whether these suggestions will prove useful, but it’s a good start.

You can see, however, that a number of different features—Snap Assist, windowed apps, virtual desktops—all flow somewhat organically into one another. I’m honestly interested to see what difference they make in my own daily workflow.

Last but not least: Search improvements

Windows 10 also adds a search button to the taskbar, moving the other major functionality of the Windows 8 Start page to the desktop environment. If you’re like me, you really don’t click icons on the Start page any more (or use bookmarks when searching the Web). Instead, “searching” for recent apps or documents is often quicker.

windows10 windows product family 9 30 eventIMAGE: MICROSOFT

Microsoft promises consistent experiences over a variety of form factors and screen sizes.

That approach is also found in Windows 10. Microsoft officials say that Search and File Explorer now displays your recent files and frequently visited folders, making finding files you’ve worked on faster and easier.

Granted, Microsoft’s Windows 10 demonstration was somewhat orchestrated to put its best face forward. But with potentially millions of eager Windows users prepared to bang away on it beginning Wednesday, any flaws will be quickly exposed. What’s refreshing is Microsoft is actually encouraging this, similar to the way in which “developers”—i.e., fans—were encouraged to take its latest Windows Phone builds for a spin.

Love it or hate it? Microsoft wants to know. But I think you’ll quite like Windows 10.

[This article was first published on PCWorld]

AngularJS-large

Create your first real-time AngularJS application

In my previous article I talked about creating real-time PHP application. That was on the server side and I demonstrated a very very basic client to connect with it. Let’s take that to next step and create a Javascript client with AngularJS.

Code

angular-client.html


<html>
 <head>
 css/bootstrap.min.css" rel="stylesheet">
 
 
 
 <script src="angular-client.js"></script>
<style>
 body { margin-top: 10px; }
 input.message { height: 30px; }
 </style>
 </head>
 AppCtrl">
 <form class="form-inline">
 <button ng-click="connect()" class="btn">Connect</button>
 <input type="text" ng-model="text" placeholder="input message to send" class="message"></input>
 <button ng-click="send()" class="btn">send</button>
 </form>
 
 <table class="table table-striped">
 <tr ng-repeat="message in messages">
 <td>{{message}}</td>
 </tr
 </table>
 </body>
</html>

 


angular-client.js


var app = angular.module('app', []);
app.factory('ChatService', function() {
 var service = {};
 
 service.connect = function() {
 if(service.ws) { return; }
 
 var ws = new WebSocket("ws://localhost:8080");
 
 ws.onopen = function() {
 service.callback("Succeeded to open a connection");
 };
 
 ws.onerror = function() {
 service.callback("Failed to open a connection");
 }
 
 ws.onmessage = function(message) {
 service.callback(message.data);
 };
 
 service.ws = ws;
 }
 
 service.send = function(message) {
 service.ws.send(message);
 }
 
 service.subscribe = function(callback) {
 service.callback = callback;
 }
 
 return service;
});
 
 
app.controller('AppCtrl', ['$scope', 'ChatService', function($scope, ChatService) {
 $scope.messages = [];
 
 ChatService.subscribe(function(message) {
 $scope.messages.push(message);
 $scope.$apply();
 });
 
 $scope.connect = function() {
 ChatService.connect();
 }
 
 $scope.send = function() {
 ChatService.send($scope.text);
 $scope.text = "";
 }
}]);


Details

It is pretty straightforward. We created an Angular Service and consumed that in our Angular controller. The only purpose of Angular service is handling communication. It will hand over the message to the subscriber in our case Angular controller and controller can do anything with that message. Here since we demonstrated the chat application so controller displays that message received.

That’s it! so simple.

Note: Both HTML and Javascript files are also available on Gist.

Code was referenced from here.

 

 

ratchet-logo

Create your first real-time PHP application

If you ever wondered if there is a way to write a real-time web application using PHP where any event or message is delivered/pushed to all recipients as they occur, in real-time? Then you are at right place. We’ll be building a sample real-time chat application using PHP and Ratchet and all messages will be pushed to all recipients in real-time. There are other technologies like node.js and socket.io but obviously there is a learning curve. If you don’t have time to learn a new thing or just want to stick with PHP then keep reading.

Goal

The goal of this application is to write a simple Chat application. Chats in event-driven programming are the “Hello World!” of applications. The chat will accept all incoming messages and deliver that message to all other connections.

The Chat class

Note: This document assumes you are familiar with PSR-0 and Composer. See Installation if you’re not yet familiar with this.

We’re going to hold everything in the MyApp namespace. Your composer file should look something like this:

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/ratchet": "0.3.*"
    }
}

We’ll start off by creating a class. This class will be our chat “application”. This basic application will listen for 4 events:

  • onOpen – Called when a new client has Connected
  • onMessage – Called when a message is received by a Connection
  • onClose – Called when a Connection is closed
  • onError – Called when an error occurs on a Connection

Given those triggers, our class will implement the MessageComponentInterface:

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) {
    }

    public function onMessage(ConnectionInterface $from, $msg) {
    }

    public function onClose(ConnectionInterface $conn) {
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

You’ll notice, in addition from just implementing methods from the MessageComponentInterface, we’ve given our application a namespace and are accepting the ConnectionInterfaceclasses. This class, usually implemented by a Connection instance, is a representation of a client’s connection on the other side of the socket. On each of the four triggered events, the client Connection representation is passed. These objects are re-used, you will receive the same Connection sometimes.

Save this as /src/MyApp/Chat.php. We’ll come back to our Chat class soon.

Instantiation

Our Chat class will be our application logic. Next, we’re going to piece together our shell script. This is the script/file we will call from the command line to launch our application.

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new Chat(),
        8080
    );

    $server->run();

Above, you’ll see we create an I/O (Input/Output) server class. It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors. The new instance of Chat class then wraps the I/O Server class. Finally, we tell the server to enter an event loop, listening for any incoming requests on port 8080.

Save this script as /bin/chat-server.php. Now, we can run it with the following command in your terminal:

$ php bin/chat-server.php

The script should now execute, taking possession of your terminal. You can cancel the script, as we’re not quite finished yet.

Logic

So far, we’ve just set up structure, both in our shell script and our Chat class. Now, to add code to our Chat to complete our application.

Let’s add some logic to our Chat class. What we’re going to do, is track all incoming Connections, in order to send them messages. Typically, you would store a collection of items in an array, but we’re going to use something called SplObjectStorage. These storage containers are built to store objects, which is what the incoming Connections are.

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}

Running It

Complete, let’s run it and test it. Open up three terminal windows, typing:

$ php bin/chat-server.php
$ telnet localhost 8080
$ telnet localhost 8080

In each of the telnet windows, type a message (“Hello World!”) and see it appear in the other!

Next Steps

Now that we have a basic working Chat application, let’s make that work in a web browser (Chrome, FireFox, or Safari [for now]). First, let’s go back to our chat-server.php script. We’re going to utilize another component of Ratchet; the WsServer class:

<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new Chat()
            )
        ),
        8080
    );

    $server->run();

Run the shell script again, open a couple web browser windows, and open a javascript console or a page with the following javascript:

var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

Once you see the console message “Connection established!” you can start sending messages to other connected browsers:

conn.send('Hello World!');

This post was extracted from Ratchet documentation.


mod_rewrite_logo

How can I test if mod_rewrite on my server is enabled and working?

Note that mod_rewrite can only be used with web server Apache. Follow the instructions below to check whether module mod_rewrite is installed and correctly configured on your server.

Create the file .htaccess and add these two lines

RewriteEngine on
RewriteRule ^testing.php$ modrewrite.php

This tells the web server to load modrewrite.php when testing.php is requested.

Create the file modrewrite.php with this line

<?php echo "mod_rewrite works"; ?>

Create the file testing.php with this line

<?php echo "mod_rewrite does not work"; ?>

Now use your web browser to load testing.php. If you see “mod_rewrite works” your server has a working mod_rewrite instance. If you see anything else, including an internal server error, your server is not properly configured for mod rewrite.

AngularJS-large

Global Variables in AngularJS

I’ve followed the angularjs tutorial and I noticed that I wasn’t able to have global variables.
Turns out to be actually simple but Angular doesn’t mention it.
You will need to edit your app module (app.js )

var app = angular.module('appName',);
//Add this to have access to a global variable
app.run(function ($rootScope) {
    $rootScope.globalVariable = 'Hi, global variabel'; //global variable
});

Now if you want to use it from your controller

function appNameCtrl($scope, $rootScope){
    $rootScope.globalVariable = 'Modji';
}

In your view

My name is {{globalVariable}}

If you are using any services like $http in your controller then you have to pass $rootScope as service along with $http.

app.controller('appNameCtrl', ['$http', '$rootScope', function ($scope, $rootScope){
    $rootScope.globalVariable = 'Modji';

}]);

To see the example visit my Plunker at http://plnkr.co/edit/JyIfkT1AxiCU2xx4WjJK?p=preview

This post is edited and was originally published at Coding Insight.
htc_one_x_photos

Android? Too many pattern attempts? Can’t hard reset? Here what to do!

htconexI had pattern screen lock on my HTC One X Android phone. Yesterday my nephew tried to unlock it too many times and it got permanently locked.

Too many pattern attempts

After trying too many attempts phone got locked and asking for my Google username and password. But unfortunately I wasn’t connected to Wifi due to which whenever I enter my login details it says invalid username and/or password. Since it tries to connect to Google server to authenticate and it couldn’t.

Master Reset

OK, since my phone wasn’t asking me for pattern and it wasn’t connected to internet so it was also not able to authenticate my logins. So doing a master/hard reset was the only option left. To hard reset phone you have to power off your phone and then turn on in recovery/bootloader mode. To do this you have to follow these steps.

  1. Press and hold the VOLUME DOWN button, and then press and hold the POWER button.
  2. Wait for the screen with the three Android images to appear, and then release the POWER and VOLUME DOWN buttons.
  3. Press VOLUME DOWN to select FACTORY RESET, and then press the POWER button.

But for this to work the Fast boot option in Settings > Battery Manager must not be selected. Oops! I remember I selected that option so I was out of luck and this didn’t work and my phone keep starting in normal mode leaving me on same login screen.

Big Problem!

So, now I was stuck and there were several problems.

  1. I couldn’t enter my pattern since it wasn’t asking due to too many attempts
  2. It wasn’t connected to Wifi neither my phone data plan so no internet due to which it wasn’t authenticating from Google.
  3. Fast boot was selected so I was unable to restart my phone in recovery mode.

Now what? I tried to Google solution but all I was getting is to go in recovery mode to hard reset my phone to make it reusable and I was unable to do that as well. Then suddenly I found  the solution!

The Solution!

Solution to this was when your phone is switched on and you are on login screen do the following:

  • Press and hold both Volume down button as well as power button until the screen goes completely black.
  • Once it is black release the power button only. Do not release the volume down button.
  • You will be booted into the bootloader mode.

Woala! That’s it! Now you can select Factory reset from the menu and wow finally I made my phone reusable again. :)