Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3617144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:35:11+00:00 2026-05-18T22:35:11+00:00

I saw the Akka module’s description says that Play has great Comet support, but

  • 0

I saw the Akka module’s description says that Play has great Comet support, but I’ve never used Comet before and I can’t find any mention of it in Play’s documentation. How does it work in Play?


I spent a few hours over two days figuring this out so I wanted to share this info for other Play beginners.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:35:12+00:00Added an answer on May 18, 2026 at 10:35 pm

    Play includes a sample Chat application which demonstrates how to use Comet. The example doesn’t explain what’s going on though, so here’s what I’ve figured out.

    Model

    In order for others to find new updates you send, they’ll need to be stored somewhere. Conceivably this could be in a cache or even in the controller itself, but the database is going to be the safest bet, so you’ll want a model. They’ll also need a way to determine which updates are new to them, which means you’ll probably want a date field (see also: Last update timestamp with JPA). The Chat example uses a simple model:

    @Entity
    public class Message extends Model {     
        public String user;
        public Date date;
        public String text;
    
        public Message(String user, String text) {
            this.user = user;
            this.text = text;
            this.date = new Date();
        }       
    }
    

    Controller

    The controller needs two methods to facilitate Comet. One where new data is posted, which doesn’t do anything special:

    public static void postMessage(String message) {
        new Message(session.get("nick"), message).save();
    }
    

    and one for retrieving updates:

    public static void newMessages() {
        List<Message> messages = Message.find("date > ?", request.date).fetch();
        if (messages.isEmpty()) {
            suspend("1s");
        }
        renderJSON(messages);
    }
    

    The key bit here is suspend("1s") which is what holds the HTTP request open, checking for new data once per second.

    View

    The view has three responsibilities — sending new data, fetching updates and then rendering those updates.

    Sending, like the corresponding controller action, doesn’t do anything special:

    $('#send').click(function(e) {
        var message = $('#message').val();
        $('#message').val('');
        $.post('@{postMessage()}', {message: message}); 
    });
    

    Fetching updates is the magic bit:

    // Retrieve new messages
    var getMessages = function() {
        $.ajax({
            url: '@{newMessages()}',
            success: function(messages) {
                $(messages).each(function() {
                    display(this);
                });
            },
            complete: function() {
                getMessages();
            },
            dataType: 'json'
        });
    }
    getMessages();
    

    getMessages() is called once to get things started, and afterwards it calls itself recursively after each successful request. It GETs the newMessages() action which looks for new messages, and if there aren’t any it holds the request open until it has something to report. When new messages are found, the JSON data is passed to a display function:

    var display = function(message) {
        $('#thread').append(tmpl('message_tmpl', {message: message}));
    }
    

    The display function applies a JavaScript Micro-Template to the JSON data to render new messages. Use of micro templates isn’t necessary, but it does work pretty well. They’re included right in the template of the page that’s going to use them:

    <script type="text/html" id="message_tmpl">
        <div class="message <%= message.user == '${session.nick}' ? 'you' : '' %> <%= message.user == 'notice' ? 'notice' : '' %>">
            <h2><%= message.user %></h2>
            <p><%= message.text.replace('\n', '<br/>') %></p>
        </div>
    </script>
    

    The type="text/html" causes browsers, search engines and screen readers to ignore the whole script block. The result is much easier to read and maintain than using jQuery to build nodes or concatenating strings. Overall it’s pretty simple once you know which bits are relevant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Saw that in some example codes, but ive never used it, unless im dynamically
Saw some JS notation today that I've never seen before, bear with me if
I saw this same question for VIM and it has been something that I
I saw some websites that (for example): if you want to view your message
I saw the docs on the Instagram developers page. http://instagram.com/developer/endpoints/ But I want to
I saw that even by writing separate commands, we can combine two different types
I saw many threads with this tittle, but no one really speak about reuse
I saw some code when studying the open source project: here . But I
I saw some html that displays the results of a PHP code just by
I saw other posts but still can't get my background image to scale correctly.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.