Reactive applications with Spring , AngularDart and Websocket
This talk is a feedback about developing reactive applications with Spring on server-side and Dart on client-side, based on OpenSnap, a HTML5 clone of SnapChat.
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive applications with Spring AngularDart and Websocket Sébastien Deleuze, Spring Framework Commiter and Dart GDE, Pivotal @sdeleuze
8 Spring Boot + Java 8 @Configuration
@EnableAutoConfiguration
@ComponentScan
public
class
Application
extends
SpringBootServletInitializer
{
//
Run
the
app
with
a
simple
java
-‐jar
opensnap.jar
public
static
void
main(String[]
args)
{
SpringApplication.run(new
Object[]{Application.class},
args);
}
10 What’s new in Spring Framework 4 § Java 8 support § WebSocket, SockJS and STOMP § AsyncRestTemplate § Generic types used as qualifier § « Bill Of Materials » Maven § @RestController § Groovy Bean Definition DSL
12 SockJS fallback options § Websocket is not always possible (proxy, IE) § Spring Framework 4 supports SockJS protocol § Client side: thin layer on Websocket API with several possible transports (Websocket, http polling, iframe …) § Server side: a simple option to activate § Must have for every large audience website
Stomp over Websocket/SockJS 13 Programming model § Websocket API too low level for application development § Stomp over Websocket/SockJS § SocketIO simplicity + the power of a real broker … § Client side: stomp.js or Dart Stomp client § Server side: methods annotated with @MessageMapping and @SubscribeMapping in you controller Server Client
15 Architecture Client Other applications Controller methods annotated with @MessageMapping and @SubscribeMapping /app/* /topic/* /queue/* Response to: /user/* a specific user /topic/* broadcast to subscribers /queue/* to the first one who retrieves the message Client Broker (embedded or external) Response Stomp message
20 Broker § Thanks to Stomp, you have the choice • SimpleBroker: pure Java, included out of the box • External broker: RabbitMQ, Appolo … ! § Why should I use an external broker? • Broadcast in cluster mode • Monitoring • Performance tuning • Interoperability • Message persistence
21 Security § HTTP based authentification • Easy to use with Spring Security • Session cookie transmitted during Websocket handshake • No Stomp credential support yet § Authorizations • Possible by implementing a custom ChannelInterceptor (cf. OpenSnap) • Inluded in future Spring Framework and Spring Security releases app:
'*':
'USER'
user:
queue:
'*':
'USER'
'error':
'ANONYMOUS'
queue:
'snap-‐created':
'ADMIN'
topic:
'user-‐authenticated':
'USER'
'snap-‐created':
'USER,ADMIN' stomp-‐security.yml
25 Dart § Structured and flexible language created to improve maintainability and efficiency of web developments § Open Source § Pending ECMA normalisation process § Current version : Dart 1.3
32 AngularDart § Could be seen just as an AngularJS port § Maybe the best Angular version currently available § Quite young, but progress very quickly § Awesome to use § Reusable Bootstrap components with AngularDart UI
public
class
Snap
{
private
int
id;
private
User
author;
private
List
recipients;
private
String
photo;
private
int
duration;
!
public
Snap(int
id,
User
author,
List
recipients,
String
photo,
int
duration)
{
this.id
=
id;
this.author
=
author;
this.recipients
=
recipients;
this.photo
=
photo;
this.duration
=
duration;
}
!
public
User
getAuthor()
{
return
author;
}
!
public
void
setAuthor(User
author)
{
this.author
=
author;
}
//
...
} 34 Domain model class
Snap
{
int
id;
User
author;
List
recipients;
String
photo;
int
duration;
35 Dart: subscription + initial data loading class
SnapService
{
List
snapsReceived
=
new
List();
StompClientService
_client;
UserService
_userService;