Hanumantharao Bitragunta – Technical Blogs http://www.easywaytech.com/blog Just another WordPress site Tue, 11 Dec 2018 09:44:51 +0000 en-US hourly 1 https://wordpress.org/?v=4.7.2 Use of Atmosphere Framework http://www.easywaytech.com/blog/index.php/2017/08/11/use-of-atmosphere-framework/ http://www.easywaytech.com/blog/index.php/2017/08/11/use-of-atmosphere-framework/#respond Fri, 11 Aug 2017 07:30:27 +0000 http://www.easywaytech.com/blog/?p=171 In a project related to Video streaming we have used Atmosphere Framework. Atmosphere framework is designed to simplify development of Asynchronous applications which include a mix of Web Socket, Comet and RESTful behaviour.

  • Asynchronous: Both parties either client or server can communicate with each other at the same time.
  • WebSockets: Support two-way communication between server & client, both can send data at the same time
  • Comet: Enables servers to send data to the client without having received a client request

Why Atmosphere?

Based on the requirement we need a framework which supports bi-directional communication so we have selected Atmosphere Framework. The requirement includes WebSockets. The main advantage of a WebSocket is that it provides two-way communication between client and server over a single TCP connection.

Generally, a web application works in a request and response pattern, client will send a request to server then server returns the response. For every request from the same client to same server, a new connection needs to be opened.

A websocket will maintain a single connection between the client and server. It also keeps the connection alive with all its clients until they disconnect.

Atmosphere supports both Server(JAVA, Scala&Groovy) and Client (Javascript)

Need for Web Sockets:

Before getting into why we need Web Sockets, it is necessary to have a look at existing techniques, which are used for duplex communication between the server and client

  • Polling: This is a method which involves client making a periodic request at a specified time interval to the server. Then server includes data available or some message in response.
  • Another technique is Streaming, for real-time data transmission. The server keeps the connection alive and active with the client until it fetches the data from server. It includes HTTP headers and increases file size, delay.
  • Long Polling: This is similar like Polling, Client and Server keeps the connection active until data is fetched or even timeout occurs, Long Polling is nothing but performance improvement but constant request may slow down the process.

All the above techniques support duplex communication. A client is always required to send a request and a server can’t send message by itself, this problem can be resolved using Web Sockets

Web Socket: Web Socket is a protocol that provides full-duplex communication. The data can travel both sides simultaneously, as per the motto of this technique, like in a hand shake, anybody can start, in technical words either the client or server anybody can send a message first.

Eg. This example will give you a clear picture; assume there is an army with a chain of command. Here Server is General, and browsers are soldiers waiting for orders (responses), Using HTTP/REST every soldier has to ask the General if there are any new orders for him, it actually burdens the General who needs to give a response to all the soldiers separately.

In case of a Web Socket General will send a message whenever he wants to and it will reach all the soldiers at once

1. Server:

To Implement an Atmosphere application, we have used @MangedService annotation in the rest layer to create the Atmosphere Service. This is the simplest way to create Atmosphere service, if we use this annotation there is no need to interact with the core Atmosphere’s API’s.

@ManagedService:

We can simply implement Atmosphere from server side as shown above, it supports Annotations over class level and method level

@Singleton: The Singleton annotation is class level annotation, it can be used to force Atmosphere to create a single, thread safe instance of a @ManagedService annotated classes. If we use this annotation we can restrict atmosphere to create single object which is annotated to @ManageService class.

Following are the multiple class level annotations

  • @Ready: It is called when the connection is fully established
  • @Message: It will be invoked when a message is ready to deliver either request/response
  • @Disconnect: It will be invoked when the client is closed the connection or a network outage
  • @Resume: It will be invoked when the connection is resumed, this principally happens with the long-polling transport.
  • @GET, @POST, @PUT To tell the framework to dispatch HTTP relevant annotated methods

Broadcaster:

A Broadcaster is responsible for delivering messages to its subscribed Atmosphere resources (clients), we can subscribe to one or more Broadcasters to get notified about events. By default single Broadcaster is created by Atmosphere and it is associated with all clients, the default Broadcaster’s Id is always “/*”, which means to invoke all clients.

We have another implemented class MetaBroadcaster, It’s really helpful when you want to broadcast messages to all or to specific users

Deliver message to all clients: metaBroadcaster.broadcastTo(“/”, “hello client”);

Deliver message to specific client(s): metaBroadcaster.broadcastTo(“/a/*”, “hello world”);

Conclusion: Atmosphere framework makes the development easier to build Asynchronous Web applications that include a mix of WebSocket, Comet and RESTful behaviour. The Atmosphere Framework is portable and can be deployed to any web server.

]]>
http://www.easywaytech.com/blog/index.php/2017/08/11/use-of-atmosphere-framework/feed/ 0