Original web design allowed only one-way client-server communication. But there are some cases in which server need to communicate with browser.

One way is to communicate from client to server after a fixed interval that is polling mechanism. But this is not the effective way even it is costly method to hit server again and again.

Google provides Channel API to do the same.

Channel API creates a connection between your application and Google servers, allowing your application to send messages to JavaScript clients in real time without the use of polling.

This is useful for applications designed to update users about new information immediately. Some example use-cases include multi-player games, or chat rooms.

Key Components : Channel API


  • JavaScript client that connects to the channel  using the channel’s unique token generated on the server.
  • Socket/Channel is created between server and client and listens on this channel

2) Server :-
  • Creats a unique channel and send to each JavaScript client so they can connect and listen to their channel.
  • Send data/messages to clients via their channels.

3) Client Id :-

  • Identifies individual JavaScript clients on the server.
  • Server sends a particular message to particular client using the client ID.


4) Tokens :-
  • Allowing the JavaScript client to connect and listen to the channel created for it.
  • The server creates one token for each client using information such as the client’s Client ID and expiration time.
  • Tokens expire after two hours and should also be treated as secret.

5) The channel
  • A channel is a one-way communication path through which the server sends updates to a specific JavaScript client identified by its Client ID.
  • The server receives updates from clients via HTTP requests, then sends the messages to relevant clients via their channels.


The message/data :-
  • Messages are sent via HTTP requests from one client to the server.
  • When a message is received, the server passes the message to the designated client via the correct channel identified by the client ID.
  • Messages are limited to 32K.

Socket
  • The JavaScript client opens a socket using the token provided by the server. It uses the socket to listen for updates on the channel.

Presence
  • The server can register to receive a notification when a client connects to or disconnects from a channel.


For more information


Code Example :


Python (Server Side)

1) Import channel api

       from google.appengine.api import channel

2) Create channel bassed on some parameter 

userId = user_id
channelId = channel.create_channel(userId)


JavaScript (Client Side)

1) Inculde Channel.js to project


<script type=“text/javascript” src=“https://talkgadget.google.com/talkgadget/channel.js”></script>


2) Get channel based on channelId created on server side

   
    var  channel = new goog.appengine.Channel(channelId);

3) Open channel

    var socket = channel.open();

4) Attach methods to sockets

       socket.onopen = function () {
           console.log(“Channel Opened.”);
       };
       socket.onmessage = function (responseData) {
         alert(‘Server Messge : ‘ + responseData.data);

       };
       socket.onerror = function () {
           console.log(‘Channel Error’);
       };
       socket.onclose = function () {
           console.log(‘Channel Close’);
       };
   };


5) Done

Categories: Google App Engine

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Google App Engine

How to get xhtml2pdf working on Google App Engine

Create folder named pdf into App Engine project folder. Copy xhtml2pdf, html5lib, reportlab folder to pdf folder. Copy six.py file to pdf folder. Create PDFHandler.py that will create the pdf file and make it to Read more…

Google App Engine

Appcfg : Download source code and logs from GAE

Appcfg :- Google SDK Command (App Engine) Download Source Code :- 1) Open Notepad and copy the following code :appcfg.py download_app -A <your_app_id> -V <your_app_version> “<output-dir>” 2) Change <your_app_id>, <your_app_version> and <output_dir> fields accordingly. You ” (quotes) for Read more…

Google App Engine

Appcfg : One click deployment to Google App Engine

Google App Engine is IaaS (Infrastructure as a Service) provided by Google to create and deploy web applications. This service will let you select the environment in which you want to deploy your application like Read more…