{"id":7678,"date":"2015-10-09T01:06:22","date_gmt":"2015-10-09T01:06:22","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/09\/guard-listen\/"},"modified":"2015-10-09T01:06:22","modified_gmt":"2015-10-09T01:06:22","slug":"guard-listen","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/09\/guard-listen\/","title":{"rendered":"guard\/listen"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/badge.fury.io\/rb\/listen.png\" \/> <img decoding=\"async\" src=\"http:\/\/travis-ci.org\/guard\/listen.png\" \/> <img decoding=\"async\" src=\"http:\/\/gemnasium.com\/guard\/listen.png\" \/> <img decoding=\"async\" src=\"http:\/\/codeclimate.com\/github\/guard\/listen.png\" \/> <img decoding=\"async\" src=\"http:\/\/coveralls.io\/repos\/guard\/listen\/badge.png?branch=master\" \/><\/p>\n<p>The Listen gem listens to file modifications and notifies you about the changes.<\/p>\n<h2>Known issues \/ Quickfixes \/ Workarounds<\/h2>\n<p>Just head over here: https:\/\/github.com\/guard\/listen\/wiki\/Quickfixes,-known-issues-and-workarounds<\/p>\n<h2>Tips and Techniques<\/h2>\n<p>Make sure you know these few basic tricks: https:\/\/github.com\/guard\/listen\/wiki\/Tips-and-Techniques<\/p>\n<h2>Features<\/h2>\n<ul>\n<li>OS-optimized adapters on MRI for Mac OS X 10.6+, Linux, *BSD and Windows, more info below.<\/li>\n<li>Detects file modification, addition and removal.<\/li>\n<li>You can watch multiple directories.<\/li>\n<li>Regexp-patterns for ignoring paths for more accuracy and speed<\/li>\n<li>Forwarding file events over TCP, more info below.<\/li>\n<li>Increased change detection accuracy on OS X HFS and VFAT volumes.<\/li>\n<li>Tested on MRI Ruby environments (1.9+ only) via Travis CI,<\/li>\n<\/ul>\n<p>Please note that:<\/p>\n<ul>\n<li>Some filesystems won\u2019t work without polling (VM\/Vagrant Shared folders, NFS, Samba, sshfs, etc.)<\/li>\n<li>Specs suite on JRuby and Rubinius aren\u2019t reliable on Travis CI, but should work.<\/li>\n<li>Windows and *BSD adapter aren\u2019t continuously and automaticaly tested.<\/li>\n<\/ul>\n<h2>Pending features \/ issues<\/h2>\n<p>Pull request or help is very welcome for these.<\/p>\n<h2>Install<\/h2>\n<p>The simplest way to install Listen is to use Bundler.<\/p>\n<pre><code>gem 'listen', '~&gt; 2.7' # this prevents upgrading to 3.x\n<\/code><\/pre>\n<h2>Usage<\/h2>\n<p>Call <code>Listen.to<\/code> with either a single directory or multiple directories, then define the \u201cchanges\u201d callback in a block.<\/p>\n<pre><code>listener = Listen.to('dir\/to\/listen', 'dir\/to\/listen2') do |modified, added, removed|\n  puts \"modified absolute path: #{modified}\"\n  puts \"added absolute path: #{added}\"\n  puts \"removed absolute path: #{removed}\"\nend\nlistener.start # not blocking\nsleep\n<\/code><\/pre>\n<h3>Pause \/ unpause \/ stop<\/h3>\n<p>Listeners can also be easily paused\/unpaused:<\/p>\n<pre><code>listener = Listen.to('dir\/path\/to\/listen') { |modified, added, removed| # ... }\n\nlistener.start\nlistener.paused? # =&gt; false\nlistener.processing? # =&gt; true\n\nlistener.pause   # stops processing changes (but keeps on collecting them)\nlistener.paused? # =&gt; true\nlistener.processing? # =&gt; false\n\nlistener.unpause # resumes processing changes (\"start\" would do the same)\nlistener.stop    # stop both listening to changes and processing them\n<\/code><\/pre>\n<p>Note: While paused, Listen keeps on collecting changes in the background &#8211; to clear them, call \u201cstop\u201d<\/p>\n<p>Note: You should keep track of all started listeners and stop them properly on finish.<\/p>\n<h3>Ignore \/ ignore!<\/h3>\n<p>Listen ignores some directories and extensions by default (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer), you can add ignoring patterns with the <code>ignore<\/code> option\/method or overwrite default with <code>ignore!<\/code> option\/method.<\/p>\n<pre><code>listener = Listen.to('dir\/path\/to\/listen', ignore: \/\\.txt\/) { |modified, added, removed| # ... }\nlistener.start\nlistener.ignore! \/\\.pkg\/ # overwrite all patterns and only ignore pkg extension.\nlistener.ignore \/\\.rb\/   # ignore rb extension in addition of pkg.\nsleep\n<\/code><\/pre>\n<p>Note: Ignoring regexp patterns are evaluated against relative paths.<\/p>\n<p>Note: ignoring paths does not improve performance &#8211; except when Polling<\/p>\n<h3>Only<\/h3>\n<p>Listen catches all files (less the ignored once) by default, if you want to only listen to a specific type of file (ie: just rb extension) you should use the <code>only<\/code> option\/method.<\/p>\n<pre><code>listener = Listen.to('dir\/path\/to\/listen', only: \/\\.rb$\/) { |modified, added, removed| # ... }\nlistener.start\nlistener.only \/_spec\\.rb$\/ # overwrite all existing only patterns.\nsleep\n<\/code><\/pre>\n<p>Note: \u2018:only\u2019 regexp patterns are evaluated only against relative <strong>file<\/strong> paths.<\/p>\n<h2>Changes callback<\/h2>\n<p>Changes to the listened-to directories gets reported back to the user in a callback. The registered callback gets invoked, when there are changes, with <strong>three<\/strong> parameters: <code>modified<\/code>, <code>added<\/code> and <code>removed<\/code> paths, in that particular order. Paths are always returned in their absolute form.<\/p>\n<p>Example:<\/p>\n<pre><code>listener = Listen.to('path\/to\/app') do |modified, added, removed|\n  # This block will be called when there are changes.\nend\nlistener.start\nsleep\n<\/code><\/pre>\n<p>or \u2026<\/p>\n<pre><code># Create a callback\ncallback = Proc.new do |modified, added, removed|\n  # This proc will be called when there are changes.\nend\nlistener = Listen.to('dir', &amp;callback)\nlistener.start\nsleep\n<\/code><\/pre>\n<h2>Options<\/h2>\n<p>All the following options can be set through the <code>Listen.to<\/code> after the directory path(s) params.<\/p>\n<pre><code>ignore: [%r{\/foo\/bar}, \/\\.pid$\/, \/\\.coffee$\/]   # Ignore a list of paths\n                                                # default: See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer\n\nignore!: %r{\/foo\/bar}                           # Same as ignore options, but overwrite default ignored paths.\n\nonly: %r{.rb$}                                  # Only listen to specific files\n                                                # default: none\n\nlatency: 0.5                                    # Set the delay (**in seconds**) between checking for changes\n                                                # default: 0.25 sec (1.0 sec for polling)\n\nwait_for_delay: 4                               # Set the delay (**in seconds**) between calls to the callback when changes exist\n                                                # default: 0.10 sec\n\nforce_polling: true                             # Force the use of the polling adapter\n                                                # default: none\n\nrelative: false                                 # Whether changes should be relative to current dir or not\n                                                # default: false\n\ndebug: true                                     # Enable Celluloid logger\n                                                # default: false\n\npolling_fallback_message: 'custom message'      # Set a custom polling fallback message (or disable it with false)\n                                                # default: \"Listen will be polling for changes. Learn more at https:\/\/github.com\/guard\/listen#listen-adapters.\"\n<\/code><\/pre>\n<p>Also, setting the environment variable <code>LISTEN_GEM_DEBUGGING=1<\/code> does the same as <code>debug: true<\/code> above.<\/p>\n<h2>Listen adapters<\/h2>\n<p>The Listen gem has a set of adapters to notify it when there are changes.<\/p>\n<p>There are 4 OS-specific adapters to support Darwin, Linux, *BSD and Windows. These adapters are fast as they use some system-calls to implement the notifying function.<\/p>\n<p>There is also a polling adapter &#8211; although it\u2019s much slower than other adapters, it works on every platform\/system and scenario (including network filesystems such as VM shared folders).<\/p>\n<p>The Darwin and Linux adapters are dependencies of the Listen gem so they work out of the box. For other adapters a specific gem will have to be added to your Gemfile, please read below.<\/p>\n<p>The Listen gem will choose the best adapter automatically, if present. If you want to force the use of the polling adapter, use the <code>:force_polling<\/code> option while initializing the listener.<\/p>\n<h3>On Windows<\/h3>\n<p>If your are on Windows, it\u2019s recommended to use the <code>wdm<\/code> adapter instead of polling.<\/p>\n<p>Please add the following to your Gemfile:<\/p>\n<pre><code>gem 'wdm', '&gt;= 0.1.0' if Gem.win_platform?\n<\/code><\/pre>\n<h3>On *BSD<\/h3>\n<p>If your are on *BSD you can try to use the <code>rb-kqueue<\/code> adapter instead of polling.<\/p>\n<p>Please add the following to your Gemfile:<\/p>\n<pre><code>require 'rbconfig'\nif RbConfig::CONFIG['target_os'] =~ \/bsd|dragonfly\/i\n  gem 'rb-kqueue', '&gt;= 0.2'\nend\n\n<\/code><\/pre>\n<h3>Getting the polling fallback message?<\/h3>\n<p>Please visit the installation section of the Listen WIKI for more information and options for potential fixes.<\/p>\n<h3>Issues and troubleshooting<\/h3>\n<p><em>NOTE: without providing the output after setting the <code>LISTEN_GEM_DEBUGGING=1<\/code> environment variable, it can be almost impossible to guess why listen is not working as expected.<\/em><\/p>\n<p>See TROUBLESHOOTING<\/p>\n<h2>Performance<\/h2>\n<p>If Listen seems slow or unresponsive, make sure you\u2019re not using the Polling adapter (you should see a warning upon startup if you are).<\/p>\n<p>Also, if the directories you\u2019re watching contain many files, make sure you\u2019re:<\/p>\n<ul>\n<li>not using Polling (ideally)<\/li>\n<li>using <code>:ignore<\/code> and <code>:only<\/code> options to avoid tracking directories you don\u2019t care about (important with Polling and on MacOS)<\/li>\n<li>running Listen with the <code>:latency<\/code> and <code>:wait_for_delay<\/code> options not too small or too big (depends on needs)<\/li>\n<li>not watching directories with log files, database files or other frequently changing files<\/li>\n<li>not using a version of Listen prior to 2.7.7<\/li>\n<li>not getting silent crashes within Listen (see LISTEN_GEM_DEBUGGING=2)<\/li>\n<li>not running multiple instances of Listen in the background<\/li>\n<li>using a file system with atime modification disabled (ideally)<\/li>\n<li>not using a filesystem with inaccurate file modification times (ideally), e.g. HFS, VFAT<\/li>\n<li>not buffering to a slow terminal (e.g. transparency + fancy font + slow gfx card + lots of output)<\/li>\n<li>ideally not running a slow encryption stack, e.g. btrfs + ecryptfs<\/li>\n<\/ul>\n<p>When in doubt, LISTEN_GEM_DEBUGGING=2 can help discover the actual events and time they happened.<\/p>\n<h2>Forwarding file events over TCP<\/h2>\n<p>Listen is capable of forwarding file events over the network using a messaging protocol. This can be useful for virtualized development environments when file events are unavailable, as is the case with shared folders in VMs.<\/p>\n<p>Vagrant uses Listen in it\u2019s rsync-auto mode to solve this issue.<\/p>\n<p>To broadcast events over TCP programmatically, use the <code>forward_to<\/code> option with an address &#8211; just a port or a hostname\/port combination:<\/p>\n<pre><code>listener = Listen.to 'path\/to\/app', forward_to: '10.0.0.2:4000' do |modified, added, removed|\n  # After broadcasting the changes to any connected recipients,\n  # this block will still be called\nend\nlistener.start\nsleep\n<\/code><\/pre>\n<p>As a convenience, the <code>listen<\/code> script is supplied which listens to a directory and forwards the events to a network address<\/p>\n<pre><code>listen -f \"10.0.0.2:4000\" # changes in current directory are sent as absolute paths\nlisten -r -f \"10.0.0.2:4000\" # changes in current directory are sent as relative paths\nlisten -v -d \"\/projects\/my_project\" -f \"10.0.0.2:4000\" # changes in given directory are also shown\n<\/code><\/pre>\n<p><em>NOTE: if you are using a gem like <code>guard<\/code> and the paths on host and guest are not exactly the same, you\u2019ll generally want to use the <code>-r<\/code> option for relative paths<\/em><\/p>\n<p>To connect to a broadcasting listener as a recipient, specify its address using <code>Listen.on<\/code>:<\/p>\n<pre><code>listener = Listen.on '10.0.0.2:4000' do |modified, added, removed|\n  # This block will be called\nend\nlistener.start\nsleep\n<\/code><\/pre>\n<h3>Security considerations<\/h3>\n<p>Since file events potentially expose sensitive information, care must be taken when specifying the broadcaster address. It is recommended to <strong>always<\/strong> specify a hostname and make sure it is as specific as possible to reduce any undesirable eavesdropping.<\/p>\n<h2>Development<\/h2>\n<ul>\n<li>Documentation hosted at RubyDoc.<\/li>\n<li>Source hosted at GitHub.<\/li>\n<\/ul>\n<p>Pull requests are very welcome! Please try to follow these simple rules if applicable:<\/p>\n<ul>\n<li>Please create a topic branch for every separate change you make.<\/li>\n<li>Make sure your patches are well tested. All specs must pass on Travis CI.<\/li>\n<li>Update the Yard documentation.<\/li>\n<li>Update the README.<\/li>\n<li>Please <strong>do not change<\/strong> the version number.<\/li>\n<\/ul>\n<p>For questions please join us in our Google group or on <code>#guard<\/code> (irc.freenode.net).<\/p>\n<h2>Acknowledgments<\/h2>\n<h2>Author<\/h2>\n<p>Thibaud Guillaume-Gentil (@thibaudgg)<\/p>\n<h2>Contributors<\/h2>\n<p>https:\/\/github.com\/guard\/listen\/graphs\/contributors<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Listen gem listens to file modifications and notifies you about the changes. Known issues \/ Quickfixes \/ Workarounds Just head over here: https:\/\/github.com\/guard\/listen\/wiki\/Quickfixes,-known-issues-and-workarounds Tips and Techniques Make sure you know these few basic tricks: https:\/\/github.com\/guard\/listen\/wiki\/Tips-and-Techniques Features OS-optimized adapters on MRI for Mac OS X 10.6+, Linux, *BSD and Windows, more info below. Detects file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7678","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7678"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7678\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}