# Comet sockets ## Using chunked responses to create Comet sockets An useful usage of **Chunked responses** is to create Comet sockets. A Comet socket is just a chunked `text/html` response containing only `"); out.write(""); out.write(""); out.close(); } }; response().setContentType("text/html"); return ok(chunks); } ``` If you run this action from a web browser, you will see the three events logged in the browser console. ## Using the `play.libs.Comet` helper We provide a Comet helper to handle these comet chunked streams that does almost the same as what we just wrote. > **Note:** Actually it does more, such as pushing an initial blank buffer data for browser compatibility, and supporting both String and JSON messages. Let’s just rewrite the previous example to use it: ``` public static Result index() { Comet comet = new Comet("console.log") { public void onConnected() { sendMessage("kiki"); sendMessage("foo"); sendMessage("bar"); close(); } }; return ok(comet); } ``` ## The forever iframe technique The standard technique to write a Comet socket is to load an infinite chunked comet response in an iframe and to specify a callback calling the parent frame: ``` public static Result index() { Comet comet = new Comet("parent.cometMessage") { public void onConnected() { sendMessage("kiki"); sendMessage("foo"); sendMessage("bar"); close(); } }; return ok(comet); } ``` With an HTML page like: ``` ``` > **Next:** [[WebSockets | JavaWebSockets]]