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

  • SEARCH
  • Home
  • 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 9193649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:13:00+00:00 2026-06-17T21:13:00+00:00

I am following this railscast that it explains how to implement a chat application.

  • 0

I am following this railscast that it explains how to implement a chat application. I have followed everything step by step, and it seems to be working until the point I started using redis for the publish/subscribe version of it to make it faster, but my application is not updating the chats now. I have to refresh the page each time a new message comes in!

Here is my code:

class MessagesController < ApplicationController
    include ActionController::Live

  def index
    @messages = Message.all
  end

  def create
    response.headers["Content-Type"] = "text/javascript"
    @message = Message.create!(params[:message].permit(:content, :name))
    $redis.publish('messages.create',@message.to_json)
  end

  def events   
    response.headers["Content-Type"] = "text/event-stream"
    redis=Redis.new
    redis.psubscribe('messages.create') do |on|
      on.pmessage do |pattern, event, data|
       response.stream.write("event: #{event}\n")
      end
    end
    rescue IOError
      logger.info "Stream closed"
    ensure
      redis.quit
      response.stream.close  
    end
end

in messages/index.html.erb

<h1>Chat</h1>

<ul id="chat">
  <%= render @messages %>
</ul>

<%= form_for Message.new, remote: true do |f| %>
  <%= f.text_field :name, placeholder: "Name" %>
  <%= f.text_field :content %>
  <%= f.submit "Send" %>
<% end %>

I have a .js file in the assets/javascript folder which takes care of the listening to server events.

var evnt = new EventSource('/messages/events');

evnt.addEventListener('messages.create', function(e){
  var message;
  message = $.parseJSON(e.data).message;
  return $('#chat').append($('<li>').text("" + message.name + ": " + message.content));
});

I am starting the redis-server on a separate terminal like this

$: redis-server

I did not install redis using brew, just downloaded a tar file and untarred it. It is recognized by the system so it should be ok.

When I try the application it does not update the chats with the new messages. I need to refresh the page to make them show. Anyhelp?

  • 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-06-17T21:13:01+00:00Added an answer on June 17, 2026 at 9:13 pm

    Seems you do not write any data to the stream, only the event type.

    redis.psubscribe('messages.*') do |on|
      on.pmessage do |pattern, event, data|
        response.stream.write("event: #{event}\n")
        response.stream.write("data: #{data}\n\n") # You are missing this line.
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have followed the following railscast about adding pdfkit to an application, and I
Following this question, it seems that it is possible to open a file from
I have a many-to-many model, following the example in this great railscast My model
I have followed the Railcast #270 has_secure_password tutorial and have that all working. For
I am using Rails 3.1 and have been using this railscast tutorial to implement
I'm following the railscast for PaperClip and have ImageMagick installed. I followed his video
I am following the Pagination with ajax railscast for my rails 3 application. Everything
I have seen this railscast here In that episode Ryan bates show data values.
I am developing an application and I am following this rails cast: http://railscasts.com/episodes/323-backbone-on-rails-part-1 Everything
Following this question and answer , I still have a bit trouble in the

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.