Page.tml
<html
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
<t:zone t:id="formZone" id="formZone">
<!-- type a chat message in this form -->
<form t:id="ajaxForm" t:type="form" t:zone="formZone">
Message: <input t:type="TextField" t:id="message" /><input type="submit" value="Send"/>
</form>
</t:zone>
<!-- this PushTarget subscribes to the '/chatTopic' topic and appends received messages to itself -->
<t:cometd.PushTarget topic="/chatTopic" event="chat" update="append" />
<!-- this template is applied to each chat message when it is received -->
<t:block t:id="messageBlock">
<h2>${message}</h2>
</t:block>
</html>
Page.java
public class PushDemo {
@InjectComponent
private Zone formZone;
@Inject
private Block messageBlock;
@Property
private String message;
@Inject
private PushManager pushManager;
// this event is fired when a message is received on the 'chatTopic' topic
Block onChat(String message) {
this.message = message;
return messageBlock;
}
// this event is fired when the form is posted
Block onSuccess() {
// broadcast the message on the 'chatTopic' topic
pushManager.broadcast("/chatTopic", message);
return formZone.getBody();
}
}
See a live demo running here
<dependencies>
<dependency>
<groupId>org.lazan</groupId>
<artifactId>tapestry-cometd</artifactId>
<!--
lookup latest version at
https://github.com/uklance/releases/tree/master/org/lazan/tapestry-cometd
-->
<version>...</version>
</dependency>
<dependency>
<groupId>org.got5</groupId>
<artifactId>tapestry5-jquery</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
...
<repositories>
<repository>
<id>tapestry-cometd</id>
<url>https://raw.github.com/uklance/releases/master</url>
</repository>
<repository>
<id>devlab722-repo</id>
<url>http://nexus.devlab722.net/nexus/content/repositories/releases</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>