Transcript
Page 1: Building Real-Time Web Applications with Vortex-Web

Real-Time Web Programming with Vortex-Web

Angelo  Corsaro,  PhD  Chief  Technology  Officer  

[email protected]

Page 2: Building Real-Time Web Applications with Vortex-Web

Vortex

Page 5: Building Real-Time Web Applications with Vortex-Web

Vortex Web

Page 13: Building Real-Time Web Applications with Vortex-Web

dds.js Overview

Page 21: Building Real-Time Web Applications with Vortex-Web

Cop

yrig

ht P

rism

Tech

, 201

4

DataCache Operations

# Writes a data sample into the cache corresponding to the key k !write: (k, data)!!# Returns a list representing the content of the cache ordered by key.!read()!!# Returns a list representing the last sample for each key!readLast()!!# Returns a list representing the content of the cache ordered by key. !# The data is actually removed from the cache.!take()!!# Returns a list representing the last sample for each key!# The data is actually removed from the cache.!takeLast()

Page 24: Building Real-Time Web Applications with Vortex-Web

Cop

yrig

ht P

rism

Tech

, 201

4

DataCache Operations

# Folds the content of the data cache using z as “zero” for the folding function f!# For instance, assuming that the operator “+” is defined for the elements of !# the cache, then you could compute the sum of all elements doing:!# c.fold(0)((a, v) -> a + v)!# the product by: !# c.fold(1)((a, v) -> a * v)!# a comma separated string representation of the content:!# c.fold(“”)(a, v) -> a + “, “ + v!fold(z)(f) !!# Register a listener l to be notified whenever data which matches a !# predicate p is written into the cache. If no predicate is provided !# then the listeners is always notified upon data insertion.!addListener(l, p)!!

Page 25: Building Real-Time Web Applications with Vortex-Web

Hacking Time!

Page 27: Building Real-Time Web Applications with Vortex-Web

Cop

yrig

ht P

rism

Tech

, 201

4

The Chat CoffeeScript#  Create  useful  alias  for  coffez  and  jQuery  root  =  this  z_  =  coffez  $  =  jQuery  !server  =  “ws://demo-­‐eu.prismtech.com:9999"  !#  The  post  type  used  by  the  chat  application  class  Post      constructor:  (@name,  @msg)  -­‐>  !#  Create  the  runtime  runtime  =  new  dds.runtime.Runtime()  !#  Define  the  Post  topic  used  to  send  and  receive  chat  posts  postTopic  =  new  dds.Topic(0,    "Post")  !#  Define  the  QoS  for  the  DataReader/Writer  drQos  =  new  dds.DataReaderQos(dds.Reliability.Reliable)  dwQos  =  new  dds.DataWriterQos(dds.Reliability.Reliable)  

Page 28: Building Real-Time Web Applications with Vortex-Web

Cop

yrig

ht P

rism

Tech

, 201

4

The Chat CoffeeScriptpostReader  =  z_.None  postWriter  =  z_.None  !avatar  =  "avatar"  +  Math.floor((Math.random()  *  10000)  +  1);  !#  Add  post  to  the  chat  and  format  it  to  show  it  is  from  me  createMyPost  =  (post)  -­‐>  ...    !#  Add  post  to  the  chat  and  format  it  to  show  it  is  from  others  createOtherPost  =  (post)  -­‐>  ...    !#  Add  post  to  the  chat  and  format  it  to  show  it  is  from  others  processPost  =  ()  -­‐>      msg  =  $("#ChatMessage").val()      post  =  new  Post(avatar,  msg)      #  Publish  the  post  (notice  that  postWriter  is  an  Option  Monad)      #  Take  a  look  at  (http://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe)      #  or  (http://www.scala-­‐lang.org/api/2.11.0/index.html#scala.Option)      postWriter.map((dw)  -­‐>  dw.write(post))      $("#ChatMessageList").append(createMyPost(post))      $("#ChatMessage").val("")  !

Page 29: Building Real-Time Web Applications with Vortex-Web

Cop

yrig

ht P

rism

Tech

, 201

4

The Chat CoffeeScript#  Deal  with  click  and  keys  events…  !$("#ChatMessage").keyup(          (e)  -­‐>              if(e.keyCode  is  13)  then  processPost()  )  !!$("#SendMsgButton").click(      (evt)  -­‐>          console.log("Send  Button  has  been  clicked")          processPost()  )  !$("#SelectAvatarButton").click(      (evt)  -­‐>          s  =  $("#AvatarName").val()          if  (s  isnt  "")              avatar  =  s  )  

Page 30: Building Real-Time Web Applications with Vortex-Web

Cop

yrig

ht P

rism

Tech

, 201

4

The Chat CoffeeScript#  Handle  the  runtime  onconnect  event  runtime.onconnect  =  ()  -­‐>      #  Create  DataReader  and  DataWriter  for  our  posts      dr  =  new  dds.DataReader(runtime,  postTopic,  drQos)      dw  =  new  dds.DataWriter(runtime,  postTopic,  dwQos)  !    #  Register  a  listener  with  the  data  reader  to  post  messages        #  in  our  chat      dr.addListener(          (post)  -­‐>              if  (post.name  isnt  avatar)                  $("#ChatMessageList").append(createOtherPost(post)))      postReader  =  z_.Some(dr)      postWriter  =  z_.Some(dw)  !connectRuntime  =  ()  -­‐>      $("#AvatarName").val(avatar)      runtime.connect(server,  "uid:pwd")  !$(document).ready(()  -­‐>  connectRuntime())  


Recommended