Tuesday, July 18, 2017

Getting Started with Redis in PHP

What is Redis?

Redis created by Salvatore Sanfilippo is an open source, in-memory data structure server with advanced key-value cache and store, often referred to as a NoSQL database. It is also referred to as a data structure server, since it can store strings, hashes, lists, sets, sorted sets, and more.

The essence of a key-value store is the ability to store some data, called a value inside a key. This data can later be retrieved only if we know the exact key used to store it.

Usage of Redis

  • Caching can be used in the same manner as memcached.
  • Leaderboards or related problems.
  • Counting stuff.
  • Real time analysis.
  • Deletion and filtering.
  • Show latest item listings in your home page.

Install on Windows

Now, let us check how to set up Redis PHP driver.

You need to download the phpredis from github repository https://github.com/nicolasff/phpredis. Once you’ve downloaded it, extract the files to phpredis directory.

extension = redis.so

Connect to Redis Server


Monday, July 17, 2017

Inter Process Communication Using PHP ,ZeroMQ AND React/ZMQ

push the external changes to existing web pages

The above diagram shows the any external php file or database layer changes has happen should be notify to all our web client. We can able to achieve this using React/ZMQ

Requirements

ZeroMQ

All Our client web pages will be listening to port 8080 for incoming WebSocket connections...but how will it also get updates from another PHP script or another server process event. Enter ZeroMQ. We could use raw sockets, like the ones Ratchet is built on, but ZeroMQ is a library that just makes sockets easier. install zmq

React/ZMQ

Ratchet is a WebSocket library built on top of a socket library called React. React handles connections and the raw I/O for Ratchet. In addition to React, which comes with Ratchet, we need another library that is part of the React suite: React/ZMQ. This library will bind ZeroMQ sockets to the Reactor core enabling us to handle both WebSockets and ZeroMQ sockets. To install, your composer.json file should look like this:

To Run the above application go to root directory "push" run below command to run the web socket connection on server side
php bin/push-server.php
Run the : "push\src\MyApp\clientwebpage.html" which connect to web socket listen to the specified category

PHP ZMQ extension Installation in Apache 2.4 & php 5.6


PHP ZMQ extension we will look at how you can easily distribute work to background processes, provide flexible service brokering for your next service oriented architecture, and manage caches efficiently and easily with just PHP and the ZeroMQ libraries. Whether the problem is asynchronous communication, message distribution, process management or just about anything, ZeroMQ can help you build an architecture that is more resilient, more scalable and more flexible, without introducing unnecessary overhead or requiring a heavyweight queue manager node..

Installing ZMQ in Windows

Required Softwares Apache 2.4 & PHP 5.6
  1. Download the zmq php extension from following url depends up on the installed php version
    url: ZMq Download Url
  2. Extract the folder
  3. Copy the php_zmq.dll to php/ext directory
  4. Then copy the libzmq.dll to the php/ root directory
  5. Add below line in php.ini file : extension=php_zmq.dll

Friday, April 7, 2017

Defining a HTML template to append using JQuery

Append Dynamic content to html template using Jquery

If we want to append the dynamic content to html template using jquery below will be the simple solution

Wednesday, April 5, 2017

Iframe communication or Accessing a cross-origin frame

Accessing a cross-origin frame.

window.postMessage allows you to send messages not only across frames (regular frame or iframe) but also across domains. This post showed interaction from parent to child. Even when we keep the iframe domain url is different for the main page url.
With out using the window.postMessage will gives us below error message in console

SecurityError: Blocked a frame with origin from accessing a cross-origin frame

You can't access an iframe with Javascript, it would be a huge security flaw if you could do it.
For the same-origin policy browsers block scripts trying to access a frame with a different origin.
Origin is considered different if at least one of the following parts of the address isn't maintained: ://:/path/to/page.html Protocol, hostname and port must be the same of your domain, if you want to access a frame.


Examples
Here's what would happen trying to access the following URLs from http://www.example.com/home/index.html

URL RESULT
http://www.example.com/home/other.html -> Success
http://www.example.com/dir/inner/another.php -> Success
http://www.example.com:80 -> Success (default port for HTTP)
http://www.example.com:2251 -> Failure: different port
http://data.example.com/dir/other.html -> Failure: different hostname
https://www.example.com/home/index.html.html -> Failure: different protocol
ftp://www.example.com:21 -> Failure: different protocol & port
https://google.com/search?q=james+bond -> Failure: different hostname & protocol
Workaround

Monday, April 3, 2017

Get Google contacts with Javascript using Google Contacts API and OAUTH 2.0

To get the Google Contacts with Javascript using Google Contacts Api

To Pull the google contacts we need to create a project in google developer console

Create a Google application in Google Developers Console for obtaining your Client id and Client secret

  • Go to Google Google Developers Console and login with your google account.
  • create one a project by clicking Create Project Button
  • Go into the newly created project
  • Click On the Menu icon and select the Api Manager Link
  • Search for “Contacts API” and click on it
  • Enable the “Contacts API” by hitting the “Enable API” button
Below is the code to pull the contacts from google relplace client id with newly created client id

Friday, March 31, 2017

Deploy files or directory hierarchies to a server using Grunt and ppk file


Grunt Sftp Deployment using ppk file

Add Grunt sftp-deploy plugin using below command

npm install grunt-sftp-deploy

Create the Gruntfile.js


Create the .ftppass The below file will contains the server connection details

With any luck, you will get a sweet deployment result like this after enter the below command
$ grunt sftp-deploy:build
>> Logging in with key at E:/credentials/keys/serverkey.ppk
>> Concurrency : 4
transferred=[1/3] elapsed=[0.0s] overall=[33%] eta=[0.0s] [====== ]
>> Directories done.
transferred=[2/3] elapsed=[0.7s] overall=[67%] eta=[0.4s] [============ ]
>> Transferred : 0 Mb
Done, without errors.