-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mqtt client is not working in background mode in a Rails application #49
Comments
I wouldn't recommend using an mqtt subscriber inside a rails app. Every time you open rails console or have multiple processes, each one will become a duplicate subscriber, probably undesirable? - I have a separate process to act as a subscriber. However, publishing MQTT messages from Rails works great! You want to add something like this in your puma.rb: on_worker_boot do
# Re-open appenders after forking the process
SemanticLogger.reopen
MyMQTT.reopen if defined?(MyMQTT)
end Inside of MyMQTT class I have: def create_client
logger.info("Starting MQTT Connection (client_id=#{@client_id} " \
"clean_session=#{@clean_session} username=#{@username} " \
"will_topic=#{@will_topic})")
PahoMqtt::Client.new(
clean_session: @clean_session, client_id: @client_id,
username: @username, password: @password,
# persistent
persistent: true, reconnect_limit: 1000, reconnect_delay: 5,
# Keep Alives
keep_alive: 29, ack_timeout: 120,
# Will
will_topic: @will_topic, will_payload: 'offline',
will_qos: 2, will_retain: true
)
end
# Method to deal with Puma workers
def reopen
logger.warn('Re-opening the connection...')
@client = create_client
@client.connect(@ip, @port)
end |
For my Rails app I am running It in a sidekick job and It's working as expected. I am also on Heroku. Here is my sidekiq_server = !!Sidekiq.redis(&:info) rescue false
if sidekiq_server
Sidekiq.configure_client do |config|
schedule_file = 'config/schedule.yml'
if File.exist?(schedule_file) && Rails.env.production?
Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
end
Rails.application.config.after_initialize do
MqttJob.perform_async
end
end
end |
I know this is old but if someone is looking for a simple implementation of this ... create a rake task like below and run from cron every minute or however often:
|
What is best practice for where to put |
is there any better way to do it? |
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 usingrails c -d
command, mqtt subscriptions are not working. Then I followed the instructions in Foreground and Daemon section, but this configclient.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 theon_connack
callback), buton_suback
callback is not giving any response.This is my MQTT handler file
`
`
And I'm calling the
handle_mqtt_subscriptions
function from myconfig/application.rb
file like:`
`
What could be the issue, any clue?
The text was updated successfully, but these errors were encountered: