Google AppEngine Java Channel API Basic Example

Hi all ,

Currently  i’m working on Google App Engine with Channel API. I just needed basic socket example. I’ll show you how to create basic example.

Steps..

  1. Setup Eclipse Google App Engine Plugin
  2. Send Message From Servlet to Client
  3. Client Listener
  4. Deploy..

How to Install Google App Engine Eclipse Plugin

Note : When you downloading Eclipse choose the EE version. Check your Eclipse version under the eclipse directory in “.eclipseproduct”. If your version is under 3.8 you’ll be able to install from local archive. Please Check the plugin that corresponds to your version of Eclipse. I’m using Kepler 4.3: https://dl.google.com/eclipse/plugin/4.3

  • Help->Install New Software  add your location and install SDK. (I only need this for socket example)
  • Restart Eclipse
  • File->New->Web Application Project
  • Done!!

Basic Java Servlet

Message Sender Servlet

package com.mtigdemir;

import java.io.IOException;

import javax.servlet.http.*;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;


@SuppressWarnings("serial")
public class ServletTest extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
	ChannelService channelService = ChannelServiceFactory.getChannelService();
	channelService.sendMessage(new ChannelMessage("logger", "Connection Testing.. "));
	}
	

}

 web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	  <servlet>
    <servlet-name>ServletTest</servlet-name>
    <servlet-class>com.bumin.ServletTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletTest</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
	<welcome-file-list>
		<welcome-file>Logger.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

Basic Client

Channel API Javascript Socket Listener

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.bumin.model.Todo" %>
<%@ page import="com.bumin.dao.Dao" %>

<%@ page import="com.google.appengine.api.channel.ChannelService" %>
<%@ page import="com.google.appengine.api.channel.ChannelServiceFactory" %>
<%@page import="java.util.ArrayList"%>


<%

ChannelService channelService = ChannelServiceFactory.getChannelService();
String token = channelService.createChannel("logger");

%>

<!DOCTYPE html>
<html>
    <head>
        <script src='//code.jquery.com/jquery-1.7.2.min.js'></script>
        <script src="/_ah/channel/jsapi"></script>
    </head>
    <body>
    
    <script>
    var token ="<%=token %>";// This will creaete unique identifier(some id created by google api + ur key)

	channel = new goog.appengine.Channel('<%=token%>');    
	    socket = channel.open();    
		
	socket.onopen = function() {$('#messages').append('<p>Connected!</p>'); };
    socket.onmessage = function(message) {
    console.log(message); 
    console.log('message' + message); 
    	$('#messages').append('<p>' + message.data + '</p>');
    };
    socket.onerror = function() { $('#messages').append('<p>Connection Error!</p>'); };
    socket.onclose = function() { $('#messages').append('<p>Connection Closed!</p>'); };        
    
    </script>
    
		LOG Messages
		<p id="messages"></p>
    </body>
</html>

 

Sign In with your Google Account

Until you want to deploy your project to Google Server this step isn’t necesary. But live is better and also free until charge limits :))

Daily Quata limits can be change day by day

Resource Free Default Limit Billing Enabled Default Limit
Daily Limit Maximum Rate Daily Limit Maximum Rate
Channel API Calls 657,000 calls 3,000 calls/minute 91,995,495 calls 32,000 calls/minute
Channels Created 100 channels 6 creations/minute Based on your budget 60 creations/minute
Channel Hours Requested 200 hours 12 hours requested/minute Based on your budget 180 hours requested/minute
Channel Data Sent Up to the Outgoing Bandwidth quota 22 MB/minute 1 TB 740 MB/minute
  • Right click your project Google-> Deploy to App Engine that’s it!!

Finally

Let’s run at local http://localhost:8888/Logger.jsp http://localhost:8888/test?message=mymessage    open together.

Channel API Final

Channel API Final

You’ll see push notification when get the “/test” route. parameter is “message”There is my Live Application : http://1-dot-southern-branch-679.appspot.com/Don’t Forget the open http://1-dot-southern-branch-679.appspot.com/Logger.jsp

Muharrem Tığdemir

Leave a Reply