Module: Tramway::Chats::Broadcast

Defined in:
lib/tramway/chats/broadcast.rb

Overview

This module provides a method to broadcast new chat messages to a Tramway Chat

Constant Summary collapse

MESSAGE_TYPES =
%i[sent received].freeze

Instance Method Summary collapse

Instance Method Details

#tramway_chat_append_message(chat_id:, type:, text:, sent_at:) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
# File 'lib/tramway/chats/broadcast.rb', line 9

def tramway_chat_append_message(chat_id:, type:, text:, sent_at:)
  raise ArgumentError, 'message_type must be :sent or :received' unless MESSAGE_TYPES.include?(type.to_sym)

  Turbo::StreamsChannel.broadcast_append_to [chat_id, 'messages'],
                                            target: 'messages',
                                            partial: 'tramway/chats/message',
                                            locals: { type:, text:, sent_at: }
end