Home>
I want to see messages in real time

"[Rails6.0] Real-time chat creation with ActionCable and Devise greedy set (revised version)"
https://qiita.com/eRy-sk/items/4c4e983e34a44c5ace27

We make community type sns with rails based on this article.
And, community creation function was added to this.

Message is not displayed at the front

It looks like the data is stored on the server.
I'm in trouble because the sent message doesn't appear.

Add community creation feature,
I confirmed the behavior after sending a message in the created community
The message is no longer updated in real time.

Before creating the community, it worked fine,

Applicable source code

app/channels/community_channel.rb

class CommunityChannel</pre>
<p><br />
app/javascript/channels/community_channel.js</p>
<pre><code>import consumer from './consumer'
$(function () {
  const chatChannel = consumer.subscriptions.create ({channel: 'CommunityChannel', community: $('# messages'). data ('community_id')}, {
    connected () {
      // Called when the subscription is ready for use on the server
    },
    disconnected () {
      // Called when the subscription has been terminated by the server
    },
    received: function (data) {
      return $('# messages'). append (data ['message']);
    },
    speak: function (message) {
      return this.perform ('speak', {
        message: message
      });
    }
  });
// keypress where data-behavior is community_speaker ...
  $(document) .on ('keypress', '[data-behavior ~ = community_speaker]', function (event) {
    if (event.keyCode === 13) {
      chatChannel.speak (event.target.value);
      event.target.value = '';
      return event.preventDefault ();
    }
  });
});


app/jobs/message_broadcast_job.rb

class MessageBroadcastJob</pre>
<p><br />
app/views/communities/show.html.erb</p>
<pre><code>">
  <% = render @messages%>

<% = label_tag: content, @ community.name%>
<% = text_field_tag: content, nil, data: {behavior: 'community_speaker'}%>


app/views/messages/_message.html.erb


  <p><% = "# {message.user.email}: # {message.content}"%></p>
What I did.

turbolinks removed.

  • Answer # 1

    It was a spelling error.
    Changed as follows.

    class MessageBroadcastJob

Related articles