Giter Club home page Giter Club logo

chokidar-socket-emitter's Introduction

chokidar-socket-emitter

a simple chokidar watcher and socket.io server which emits file system events to all connected socket.io clients. Primarily it should serve as an event source for systemjs-hot-reloader but you can use it any other way.

Install

npm i -D chokidar-socket-emitter

CLI usage

npm i -g chokidar-socket-emitter
chokidar-socket-emitter -l 1234

available CLI options:

-l, --port <n>
-p, --path <path>
-d, --dir <n>
-P, --poll  # use when you have a disk mounted over network for example
-q, --quiet # don't print out any logs

By default listens on port 5776.

NPM script usage

Combined with browser-sync:

"scripts": {
  "start": "npm run serve & npm run watch",
  "serve": "browser-sync start --server",
  "watch": "chokidar-socket-emitter"
},

Start by running:

npm start

Programatic usage

var chokidarEvEmitter = require('chokidar-socket-emitter')
chokidarEvEmitter({port: 8090}) //path is taken from jspm/directories/baseURL or if that is not set up, '.' is used
//or specify the path
chokidarEvEmitter({port: 8090, path: '.'})

//you can also supply an http server instance, that way it will run within your server, no need for extra port
require('chokidar-socket-emitter')({app: server})

FAQ

Does chokidar have problems with watching drives mounted from VMs hosts/network?

Yes and if you want it to work, use additional opts property to switch to polling mode

chokidarEvEmitter({port: 8090, path: '.', chokidar: {usePolling: true}})

chokidar-socket-emitter's People

Contributors

alteredconstants avatar capaj avatar guybedford avatar johnraz avatar kwesterfeld2 avatar nickbalestra avatar peteruithoven avatar svi3c avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

chokidar-socket-emitter's Issues

CPU spike if jspm_packages is not in root dir

Using baseUrl, the socket emitter correctly finds content; however, the exclusion for jspm_packages/** watch does not take this into account, and spikes the CPU on my system b/c of sheer number of dependencies. Would rather not have to move my directories around to support this.

default ignored dirs no longer ignored after path resolution

This commit changed the path resolution.

Afterwards, files within node_modules and probably jspm_packages are no longer ignored by default, because they don't lie within the root/baseURL directory anymore:

C:\Source\misc\choki-test> npm i -g [email protected]
C:\Source\misc\choki-test> chokidar-socket-emitter
chokidar watching  C:\Source\misc\choki-test
chokidar-socket-emitter listening on 5776
// Changing file within node_modules emits no event (correct default behavior)
^CTerminate batch job (Y/N)?


C:\Source\misc\choki-test> npm i -g [email protected]
C:\Source\misc\choki-test> chokidar-socket-emitter
chokidar watching  C:\Source\misc\choki-test
chokidar-socket-emitter listening on 5776
File  C:/Source/misc/choki-test/node_modules/brace-expansion/package.json  emitted: change
File  C:/Source/misc/choki-test/node_modules/brace-expansion/package.json  emitted: change
File  C:/Source/misc/choki-test/node_modules/brace-expansion/package.json  emitted: change
File  C:/Source/misc/choki-test/node_modules/brace-expansion/package.json  emitted: change

This also probably leads to systemjs-hot-reloader no longer recognizing the changed files.

When chokidar-socket-emitter running via npm it fails

Missing error handler on socket.
TypeError: fn is not a function
at Socket. (/Volumes/HDD/Projects/angular2/crack/node_modules/chokidar-socket-emitter/server.js:62:7)

I belive that correct will be

socket.on('package.json', function (fn) {
      fn(pjson)
    })

baseURL support

Surely in order to support baseURL, this would need to have knowledge of the jspm configuration file etc?

Error: watch jspm_packages/npm/[email protected]/lib/__mocks__/fetch.js ENOSPC

On Elementary OS I get the following error when starting the following script:

'use strict';
const httpServer = require('http-server');
const server = httpServer.createServer({
  cache: -1,
  robots: true,
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Credentials': 'true'
  }
});

require('chokidar-socket-emitter')({ app: server.server });

server.listen(3000);

console.log('Visit: localhost:3000');
$ nodejs server.js 
Visit: localhost:3000
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: watch jspm_packages/npm/[email protected]/flow/include/nullthrows.js ENOSPC
    at exports._errnoException (util.js:870:11)
    at FSWatcher.start (fs.js:1234:19)
    at Object.fs.watch (fs.js:1262:11)
    at createFsWatchInstance (/home/peteruithoven/Projects/Doodle3D/dev/Doodle3D-App/node_modules/chokidar-socket-emitter/node_modules/chokidar/lib/nodefs-handler.js:37:15)
    at setFsWatchListener (/home/peteruithoven/Projects/Doodle3D/dev/Doodle3D-App/node_modules/chokidar-socket-emitter/node_modules/chokidar/lib/nodefs-handler.js:80:15)
    at FSWatcher.NodeFsHandler._watchWithNodeFs (/home/peteruithoven/Projects/Doodle3D/dev/Doodle3D-App/node_modules/chokidar-socket-emitter/node_modules/chokidar/lib/nodefs-handler.js:228:14)
    at FSWatcher.NodeFsHandler._handleFile (/home/peteruithoven/Projects/Doodle3D/dev/Doodle3D-App/node_modules/chokidar-socket-emitter/node_modules/chokidar/lib/nodefs-handler.js:255:21)
    at FSWatcher.<anonymous> (/home/peteruithoven/Projects/Doodle3D/dev/Doodle3D-App/node_modules/chokidar-socket-emitter/node_modules/chokidar/lib/nodefs-handler.js:473:21)
    at FSReqWrap.oncomplete (fs.js:82:15)

Could it be that it's also trying to watch all the files in node_modules and jspm_packages?

Simple Way of Excluding Files / Paths

Use case would be to exclude .git to prevent events like this on commit:

File  .git/COMMIT_EDITMSG  emitted:  change
File  .git/logs/refs/heads/feature/compose  emitted:  change
File  .git/logs/HEAD  emitted:  change
File  .git/objects/4d  emitted:  addDir
File  .git/objects/4d/cae0aeb2d360fd662f417ba9fd5d7291462267  emitted:  add
File  .git/objects  emitted:  addDir
File  .git/objects/7a/48c4e14815f8a7846a629fece316feb99dd3bc  emitted:  add
File  .git/objects/7a  emitted:  addDir
File  .git/objects/c3/7b83e6d24f3d76cbcce21137c0a31a34cb8089  emitted:  add
File  .git/objects/c3  emitted:  addDir
File  .git/objects/74/daac50b0488983ea3c7779d4b7084f8d36875d  emitted:  add
File  .git/objects/74  emitted:  addDir
File  .git/objects/e3/dba50f2fdd2438bff9cf09967d6c0993ab19bc  emitted:  add
File  .git/objects/e3  emitted:  addDir
File  .git/index  emitted:  change
File  .git/objects/c4/51eb9f187fbe06675f9302def1ab472a127b1b  emitted:  add
File  .git/objects/c4  emitted:  addDir
File  .git/refs/heads/feature/compose  emitted:  change
File  .git/FETCH_HEAD  emitted:  change
File  .git/FETCH_HEAD  emitted:  change
File  .git/logs/refs/remotes/origin/feature/compose  emitted:  change
File  .git/refs/remotes/origin/feature/compose  emitted:  change

Errors in Windows

When I run it with gulp in windows I get the following error:

cat@WINDOWS C:\dev\Web
$ gulp hmr
[08:02:28] Using gulpfile C:\dev\Web\gulpfile.js
[08:02:28] Starting 'hmr'...
[08:02:28] 'hmr' errored after 1.21 ms
[08:02:28] Error: Cannot find module 'C:\Users\cat\AppData\Roaming\nvm\v5.6.0\node_modules\gulp\bin\package.json'
  at Function.Module._resolveFilename (module.js:339:15)
  at Function.Module._load (module.js:290:25)
  at Module.require (module.js:367:17)
  at require (internal/module.js:16:19)
  at module.exports.chokidar.watch.on.io.on.close (C:\dev\Web\node_modules\chokidar-socket-emitter\server.js:8:17)
  at Gulp.<anonymous> (C:\dev\Web\tasks\serve.js:36:3)
  at module.exports (C:\dev\Web\node_modules\orchestrator\lib\runTask.js:34:7)
  at Gulp.Orchestrator._runTask (C:\dev\Web\node_modules\orchestrator\index.js:273:3)
  at Gulp.Orchestrator._runStep (C:\dev\Web\node_modules\orchestrator\index.js:214:10)
  at Gulp.Orchestrator.start (C:\dev\Web\node_modules\orchestrator\index.js:134:8)
  at C:\Users\AppData\Roaming\nvm\v5.6.0\node_modules\gulp\bin\gulp.js:129:20
  at nextTickCallbackWith0Args (node.js:453:9)
  at process._tickCallback (node.js:382:13)
  at Function.Module.runMain (module.js:449:11)
  at startup (node.js:140:18)
  at node.js:1001:3

my task is ultra simple:

gulp.task('hmr', function() {
  chokidarEvEmitter({port: 8090});
});

versions of everything are:

  • NodeJS 5.6.0
  • NPM 3.6.0
  • Gulp 3.9.1
  • chokidar-socket-emitter 0.4.2

it does seem to work fine on windows and you can observe that with this demo project: https://github.com/capaj/NG6-starter/tree/jspm .

Note: If I run it from the command line installed globally it does work fine.

Undefined "package.json".

I'm using "chokidar-socket-emitter" in conjunction with "[email protected]". I used to start the "chokidar-socket-emitter" globally, but have now switched to initializing it within a node server (ran by jspm (jspm run {my server file}).

I import it like so:

import chokidarEvEmitter from 'chokidar-socket-emitter'
chokidarEvEmitter( { app: server } )

Everything works perfectly expect that the following error is thrown by "[email protected]":
Uncaught TypeError: Cannot read property 'jspm' of null hot-reloader.js:41.

Upon further investigation, it's clear that the issue originates in the "[email protected]/server.js" file, lines 10-13:
try { pjson = require(path.join(opts.dir || path.dirname(require.main.filename), 'package.json')) } catch (err) { error = err }

Without supplying the "dir" parameter, the require fails and throws this error:
TypeError: Cannot read property 'filename' of undefined

Supplying the "dir" parameter, the require fails and throws this error:
[Error: Module /home/marcbraulio/Projects/jspm-test-v3/package.json not declared as a dependency of file:///home/marcbraulio/Projects/jspm-test-v3/jspm_packages/npm/[email protected]/server.js]

Explicitly calling:
require("/home/marcbraulio/Projects/jspm-test-v3/package.json")
results in:
SyntaxError: Unexpected token :
...somehow it fails to parse that json file, but parses:
require("/home/marcbraulio/Projects/jspm-test-v3/jspm_packages/npm/[email protected]/package.json")
...just fine (very confused about that, would love an explanation).

Also, not sure how/why
require(path.join(opts.dir || path.dirname(require.main.filename), 'package.json'))
which renders to (with the supplied opts.dir):
require("/home/marcbraulio/Projects/jspm-test-v3/package.json")
throws a different error than when you just call:
require("/home/marcbraulio/Projects/jspm-test-v3/package.json")
explicitly.

How can this be resolved and what's the reason for this inconsistent require/path/error behavior? Is it a jspm (0.17.0-beta.12) problem?

Thank you for your time.

Exception when client connects with 0.5.0 release

I just tried the 0.5.0 release with my PR merged in, and am having a new issue. I am starting the server and getting this exception:

connected client: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36
Missing error handler on `socket`.
TypeError: fn is not a function
    at Socket.<anonymous> (/Users/..../ui/node_modules/chokidar-socket-emitter/server.js:66:7)
    at emitTwo (events.js:100:13)
    at Socket.emit (events.js:185:7)
    at Socket.onevent (/Users/..../ui/node_modules/socket.io/lib/socket.js:335:8)
    at Socket.onpacket (/Users/..../ui/node_modules/socket.io/lib/socket.js:295:12)
    at Client.ondecoded (/Users/..../ui/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/Users/..../ui/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/Users/..../ui/node_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (/Users/..../ui/node_modules/socket.io/lib/client.js:175:18)
    at emitOne (events.js:90:13)

I wiped my node_modules directory and re-pulled, same issue. When a client connects, this error occurs.

Make logging optional

At the moment we cannot control the logging. There are a lot of logs to the command prompt.
This may be fine for a standalone server, but if we integrate this, it can be quite annoying.
We could add a flag to the configuration?
Does this make sense to you?

required node version not made clear

Today I used this module with success inside a grunt connect task to notify the app of changes in javascript file so that systemjs-hot-reloader will reimport modules managed with jspm.

One obstacle was that the code of this module uses ES6 features and I believe this requires node 4+ and could also be compatible with io.js. But I have not researched the io.js specifics.

Perhaps the simplest change would be to tip off the next user by adding

  "engines": {
    "node": ">=4"
  },

to package.json.

I just read this post about engines and engineStrict.

My summary: The suggested change would result in a warning only at the moment it is installed as package. So npm install chokidar-socket-emitter will give warning but not npm install when working on this package.

Adding

  "engineStrict": true.

would cause this to result in an error.

I haven't not actually tested the above changes yet.

An alternative would be to build this component so that it is transpiled to ES5 perhaps when published to npm's public registry.

In addition this could be mentioned on the README page.

Supplying current socket.io

Some of my projects already have socket.io setup. To make things easier I have been trying out a fork that accepts an io instance in opts when available.

Is there an interest in a PR for this behavior. I can clean up and submit something if wanted.

Allow custom ignore paths for chokidar

Hi!

I understand that watching jspm_packages creates issues on many systems, but my workflow depends on linking a lot of jspm packages and testing them inside a larger "sandbox", because they are often not able to run on their own.

Would you be open to an override property/option to allow the passing of a custom ignored paths array? I could create a pull request if you would be interested.

Detect is emitter already running

Thanks for the lib!

I wonder, is it possible somehow to detect is emitter already running? Or make so that invoking emitter second time (when it is already running) will not fail and simple ignore invocation?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.