[JAVA] Chat function that is updated immediately (implementation of Action Cable)

Chat app that updates instantly

I will shift the code all together ⬇︎

Terminal


% cd projects
% rails _6.0.0_ new mini_talk_app -d mysql
% cd mini_talk_app
% rails db:create

Terminal


% rails g controller messages new
% rails g model message text:text
% rails db:migrate

app/config/routes.rb


Rails.application.routes.draw do
  root 'messages#new'
  resources :messages, only: [:create]
end

app/controllers/messages_controller.rb


class MessagesController < ApplicationController
  def new
    @messages = Message.all
    @message = Message.new
  end

  def create
    @message = Message.new(text: params[:message][:text])
  end
end

java:app/views/messages/new.html.erb


<h3>mini_talk_app</h3>
<%= form_with model: @message do |f| %>
  <%= f.text_field :text %>
  <%= f.submit 'Send' %>
<% end %>
<div id='messages'>
  <% @messages.reverse_each do |message| %>
    <p><%= message.text %></p>
  <% end %>
</div>

At this point it should look like this

d6806db02ad04b485928bbd124c73f1a.png

Implementation of Action Cable (continued from earlier)

Terminal


% rails g channel message

app/channel/message_channel.rb


class MessageChannel < ApplicationCable::Channel
  def subscribed
    stream_from "message_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

app/controller/messages_controller.rb


class MessagesController < ApplicationController
  def new
    @messages = Message.all
    @message = Message.new
  end

  def create
    @message = Message.new(text: params[:message][:text])
    if @message.save
      ActionCable.server.broadcast 'message_channel', content: @message
    end
  end
end

app/javascript/channels/message_channel.js


import consumer from "./consumer"

consumer.subscriptions.create("MessageChannel", {
  connected() {
    // Called when the subscription is ready for use on the server
  },

  disconnected() {
    // Called when the subscription has been terminated by the server
  },

  received(data) {
    const html = `<p>${data.content.text}</p>`;
    const messages = document.getElementById('messages');
    const newMessage = document.getElementById('message_text');
    messages.insertAdjacentHTML('afterbegin', html);
    newMessage.value='';
  }
});

That's all from the scene!

Recommended Posts

Chat function that is updated immediately (implementation of Action Cable)
[Rails 6] Implementation of inquiry function using Action Mailer
Implementation of search function
Implementation of pagination function
Implementation of sequential search function
Implementation of like function (Ajax)
[Rails] Implementation of category function
Implementation of category pull-down function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
Real-time comment function with Action Cable (2/2)
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Real-time comment function with Action Cable (1/2)
Implementation of like function in Java
Summary: Implementation of a function that switches the logical value when a link is clicked [for own output]
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
Where the follow function implementation is stuck
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Java] Where is the implementation class of annotation that exists in Bean Validation?
Implementation of vertical and horizontal scrolling that is often seen on Android recently