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

Leave Comment

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

en_USEnglish
en_USEnglish