Server-Side Programming Primer:
REST
Web Sockets
Server-sent Events
This presentation has been developed in the context of the Mobile Applications Development course at the Computer Science Department of the University of L’Aquila (Italy).
anything that is anything that is anything that is addressable over the Web addressable over the Web addressable over the Web addressable over the Web addressable over the Web addressable over the Web addressable over the Web addressable over the Web Addressable Addressable Addressable Addressable = = = = anything that can be accessed and transferred between clients and servers a resource must have a unique address over the Web Under HTTP these are URIs URIs URIs URIs ex. .../orderinfo?id=123
is sent back and forth between clients and servers A URL URL URL URL is a specialization of URI that defines the network location of a specific resource es. http://some.domain.com/orderinfo?id=123
Java API for RESTful RESTful RESTful RESTful Web Services Web Services Web Services Web Services It is a Java programming language API that provides support in creating web services according to the REST architectural style Many Implementations Apache CXF Many Implementations Apache CXF Restlet RESTEasy Jersey Wink
Production Quality Jersey provides the connectors for web services Jersey provides the connectors for web services through Java annotations Java annotations Java annotations Java annotations https://jersey.dev.java.net
resources just as easily as we develop Plain Old resources just as easily as we develop Plain Old Java Objects (POJOs) Jersey manages: • interception of HTTP requests • representation of negotiations • representation of negotiations we can concentrate on the business rules only https://jersey.dev.java.net
service is handled by an appropriately annotated method in a resource by an appropriately annotated method in a resource class The name of the method is not important
payload payload that the framework intercepts and delivers to us in the parameter intercepts and delivers to us in the parameter payload The payload can be a string, but also a binary stream of MIME type image/jpg, so our object type for the payload must change accordingly (InputStream, for instance)
request has a payload payload payload payload associated with it, which is stored in the payload payload payload payload associated with it, which is stored in the payload variable * we will see later what @Consumes and @PathParam mean
a variable in a URI We have to define also one or more String objects as parameters of the method here, id is just another variable within the scope of the method
representations from the clients inbound representations from the clients The client sets the Content-Type HTTP header and Jersey The client sets the Content-Type HTTP header and Jersey delegates the request to the corresponding method This annotation works together with @POST and @PUT
representations to the clients representations to the clients The client sets the Accept HTTP header that maps The client sets the Accept HTTP header that maps directly to the Content-Type the method produces This annotation works together with @GET, @POST and @PUT
values of name/value pairs passed in as part – it lets us read the values of name/value pairs passed in as part of a POST or PUT request • @HEAD – the method will process HTTP HEAD request • @CookieParam – it is used to extract values from a cookie • @HeaderParam – it is used to extract parameters from an HTTP header • ...
no message event connect event connect no message http://slidesha.re/LeNohX no message connect no message event connect event http://slidesha.re/LeNohX
implement via REST • Easy to implement via REST • Runs on standard HTTP servers CONs CONs CONs CONs • No real-time user experience • Wasted bandwidth • Wasted bandwidth – most requests return no data • High server loads
Real-time user experience • Real-time user experience CONs CONs CONs CONs • High server loads • High server loads – memory – threads & processes • Still waste of bandwidth
- - -duplex communication duplex communication duplex communication duplex communication between devices and server Specifically suited for chat, videogames, drawings sharing, real-time info W3C/IETF standard W3C/IETF standard (http://dev.w3.org/html5/websockets) Requires a Web Socket Server to handle the protocol
Client notifies Client notifies websocket websocket websocket websocket server server server server of an event, giving ids of recipients recipients 2. The server server server server notifies all the active clients notifies all the active clients notifies all the active clients notifies all the active clients (subscribed to that type of event) 3. 3. 3. 3. Clients process event Clients process event Clients process event Clients process event when given recipient Id matches the client’s one http://bit.ly/Ixcupi
latest latest latest latest browsers browsers browsers browsers only • supported by latest latest latest latest browsers browsers browsers browsers only • some proxies proxies proxies proxies may still block WS handshakes • need for keep keep keep keep- - - -alive alive alive alive messages messages messages messages • need to manually manage message message message message queues queues queues queues • every encountered problem results in closing closing closing closing the the the the connection connection connection connection connection connection connection connection you cannot distinguish between: • client or server errors • network errors • timeouts
connection persistent http connection persistent http connection which has to be setup only once which has to be setup only once It is unidirectional unidirectional unidirectional unidirectional: : : : server client SSEs are sent over traditional HTTP SSEs are sent over traditional HTTP SSEs are sent over traditional HTTP SSEs are sent over traditional HTTP it can be easily implemented with standard server- side technologies (eg PHP)
sends a request sends a request to the server via HTTP 2. The server creates a process, which fetches latest state in 2. The server creates a process, which fetches latest state in the DB and responds back responds back responds back responds back 3. Client gets server response gets server response gets server response gets server response 4. In X seconds client automatically sends next request sends next request sends next request sends next request to the server http://bit.ly/Ixcupi
only generic messages generic messages var source = new EventSource(“http://some.url”); var handler = function(event){ console.log(event.data); console.log(event.id); console.log(event.origin); these are all the properties of an console.log(event.origin); console.log(event.lastEventId); } source.addEventListener(‘myEvent', handler, false); properties of an event
data (required) data (required) – the information to be sent • event event event event – the type of event being sent • id id id id – an identifier for the event to be used when the client – an identifier for the event to be used when the client reconnects • retry retry retry retry – how much time (in milliseconds) should pass before the client tries to reconnect to the URL
browsers browsers only • browser browser browser browser discrepancies discrepancies discrepancies discrepancies • browser browser browser browser discrepancies discrepancies discrepancies discrepancies • you need a server that can handle large numbers of large numbers of large numbers of large numbers of simultaneous connections simultaneous connections simultaneous connections simultaneous connections • legacy browsers may drop drop drop drop the HTTP connection the HTTP connection the HTTP connection the HTTP connection after a short timeout