Skip to content

Mqtt client is not working in background mode in a Rails application #49

@avishekjana

Description

@avishekjana

Hi,
When I'm starting my rails application using rails c command, mqtt client is working fine, and I'm receiving messages from my subscriptions perfectly. But when I'm trying to detach the rails app from console using rails c -d command, mqtt subscriptions are not working. Then I followed the instructions in Foreground and Daemon section, but this config client.connect('iot.eclipse.org', 1883, client.keep_alive, client.persistence, true) is not working. Means it's getting connected to my mqtt server (checked using the on_connack callback), but on_suback callback is not giving any response.

This is my MQTT handler file

`

require 'paho-mqtt'
class MqttHandler

  def self.handle_mqtt_subscriptions
    # Setup MQTT client
    client = PahoMqtt::Client.new({ username: "xxxxx", password: "xxxxxx", persistent: true })

    ### Register a callback trigger on the reception of a CONNACK packet
    client.on_connack = proc { puts "Connected to MQTT>>" }

    client.add_topic_callback("device/status/+") do |packet|
      puts "MQTT CHANNEL:: #{packet.topic} \n>>> #{packet.payload}"
    end

    ### Register a callback on suback to assert the subcription
    waiting_suback = true
    client.on_suback do
      waiting_suback = false
      puts "Subscribed to MQTT>>>"
    end

    ### Connect to the eclipse test server on port 1883 (Unencrypted mode)
    client.connect('mqtt.mymqttserver.in', 1883, client.keep_alive, client.persistent, true)

    ### Subscribe to a topic
    client.subscribe(['device/status/+', 1])

    ### Waiting for the suback answer and execute the previously set on_suback callback
    while waiting_suback do
      sleep 0.001
    end
  end
end

`

And I'm calling the handle_mqtt_subscriptions function from my config/application.rb file like:
`

  config.after_initialize do
      MqttHandler.handle_mqtt_subscriptions
  end

`

What could be the issue, any clue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions