alallier / reload

node module to reload your browser when your code changes
MIT License
295 stars 48 forks source link

koa #15

Open adriangrigore opened 9 years ago

adriangrigore commented 9 years ago

Consider adding support for koa?

jprichardson commented 9 years ago

Sure. PR welcomed.

On Tuesday, April 14, 2015, Adrian Grigore notifications@github.com wrote:

Consider adding support for koa?

— Reply to this email directly or view it on GitHub https://github.com/jprichardson/reload/issues/15.

Simple & Secure Bitcoin Wallet: https://www.coinbolt.com Bitcoin / JavaScript: http://cryptocoinjs.com Follow JP Richardson on Twitter: https://twitter.com/jprichardson

nahtnam commented 7 years ago

+1 for this, would love to see support for koa.

nahtnam commented 7 years ago

@jprichardson I am working on a PR, and it seems to work, but I get these errors in the log.

reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I don't know this is because reload is waiting for the server to reboot or if there is an error in the websocket implementation with koa. Here is the entire log.

reload.js:7 Reload Script Loaded
reload.js:25 Page Loaded - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:48 Socket Opened
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED // IS THIS BECAUSE THE SERVER IS RESTARTING OR BECAUSE OF AN ERROR?
(anonymous) @ reload.js:90
reload.js:80 Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED // IS THIS BECAUSE THE SERVER IS RESTARTING OR BECAUSE OF AN ERROR?
(anonymous) @ reload.js:90
reload.js:80 Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED // IS THIS BECAUSE THE SERVER IS RESTARTING OR BECAUSE OF AN ERROR?
(anonymous) @ reload.js:90
reload.js:80 Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:48 Socket Opened
reload.js:54 Reloaded
reload.js:33 Navigated away from the current URL
Navigated to http://localhost:3000/
reload.js:7 Reload Script Loaded
reload.js:25 Page Loaded - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:48 Socket Opened
alallier commented 7 years ago

It should wait for the server to be running after a restart of the server before it tries to open a socket connection. It those cases where the error is printing is the server started? The code you are using would be helpful to help you debug

nahtnam commented 7 years ago

@alallier So I got it working, but there is one problem. Koa makes use of async/await which pushes the node requirement to 7.6.0. I dont think it would be a good idea to include it in this library, unless you want to transpile the code.

ghost commented 7 years ago

The travis-ci tests try to build v4 of node, so I don't think that bumping it up to 7.6 is going to be a good idea. I'm only using 6.10.2 at the moment because that is the stable release, and that is what most people would be using

ghost commented 7 years ago

Although, it installed on my version of node (6.10.2) so maybe you can use koa, just not the async. on https://www.npmjs.com/package/koa it says that you can use either the async function or the common function. Maybe this will work, just using the common function rather than the async.

ghost commented 7 years ago

@alallier What are we doing about this?

nahtnam commented 7 years ago

Sorry, I have turned around and started working in other languages. I do not think I will be able to complete this.

ghost commented 7 years ago

@adriangrigore Do you think that you can do anything about this?

alallier commented 7 years ago

@nahtnam That is okay. Do you mind if we take your work (from #90) and adapt and build from it?

nahtnam commented 7 years ago

Of course not, please go ahead and do anything you would like. I could also answer any questions you have.

Most of the code you will need is here: https://github.com/alallier/reload/pull/90/commits/6c15d717cf853547323dc08a0d0d8dc972ca6111

It would be a good idea to tell anyone using koa to put the middleware near the top in the README.

AckerApple commented 7 years ago

I studied Koa to find it can process middleware just as Express processes middleware.

The problem with reload, is it straps itself to Express and/or to Koa INSTEAD of offering a middleware plugin to allow others to strap it to there.

I solved this in my fork by removing Express and offering a middleware function as seen here: https://github.com/AckerApple/reload/blob/master/lib/reload.js#L47

adriangrigore commented 7 years ago

@yamboy1 Sorry, didn't get the chance to even build anything with koa, was just experimenting at the time.I don't think I can be of much use.

mkalam-alami commented 4 years ago

Here is a small wrapper to pretend koa-router is actually an Express server:


function wrapRouterForReload(router) {
  const wrappedRouter = function() { }
  wrappedRouter.get = (route, callback) => {
    router.get(route, (ctx) => {
      callback(undefined, {
        type: (type) => ctx.type = type,
        send: (body) => ctx.body = body
      });
    });
  }
  return wrappedRouter;
}

You can then do:

const wrappedRouter = wrapRouterForReload(router);
reloader = reload(wrappedRouter);

Obviously this may break with future reload versions.