Use an ActionCableClient connection for logging and receiving data alongside the main application

115 Views Asked by At

when I start my application, I want ActionCable to transfer the logging to the server with a constant connection.

With this connection i want to interact with - sending actions and reading received messages on demand.

Since I have hardly dealt with EventMachine so far and found nothing on the Internet about my problem, here my question.

I will describe my problem/solution with a mix of real ruby and ruby pseudo code.

require 'action_cable_client'

my_thread = Thread.new do
  EventMachine.run do
    @@message_storage = []
    uri = "ws://127.0.0.1:3000/cable"
    client = ActionCableClient.new(uri, 'LoggingChannel')

    client.connected do
      puts 'successfully connected.'
    end

    client.disconnected do
      puts 'disconnected.'
    end

    client.received do |message|
      puts "Got a message: #{message}"
      @@message_storage += [message]
    end
  end
end

# here is my main app loop

some_actions()

if error
  my_thread.client.perform('error', {message: 'error 123 occured'})
end

if warning
  my_thread.client.perform('info', {message: 'some information for you'})
end

puts "Currently there are #{my_thread.@@message_storage.size} messages in storage"

Is there a solution?

Additional information, my intention:

I didn't figure out (so there is only pseudo code for it, that logically looks fine)

  • how i can interact with the ActionCable inside the EventMachine to perform a task in it from outside
  • how i can access a value inside the EventMachine/ActionCable from outside
0

There are 0 best solutions below