socketstream/ss-examples

Combine Koa with SS

Closed this issue · 12 comments

How do I prepend/append (?) Koa to the SS stack? I know how to do this w/ Express, but it's a little different w/ Koa since the middleware runs on generators not callbacks. Koa does however, supply a app.callback() function (which happens after running all the generators)—can this be passed? How? What would the code look like?

Okay, so I did a little hooking up. It had the reverse effect I expected. I actually got Koa routes running, but it killed SS routes. This line specifically: https://github.com/socketstream/ss-examples/blob/feature/ractive-example/ractive-koa/app.js#L33

Any clues? If I comment that line out, Koa stops working and SS starts working again.

I'm wondering if it's just a conflict between generators (Koa's approach) and callbacks (SS's approach). Any thoughts welcome.

I think SS can be improved so it feels less of a framework in this area. In production I'd like to see all client files served fully static leaving just the streaming which should allow for several options for server configuration.

I wonder if this is similar to Express configuration.

In Express I have

 var app = express();
 ...
 app.use('/',ss.http.middleware);
 ....
 var httpServer = app.listen(config.port);
 ss.start(httpServer);

I think SS can be improved so it feels less of a framework in this area. In production I'd like to see all client files served fully static leaving just the streaming which should allow for several options for server configuration.

@thepian this comment doesn't make sense to me. what exactly do you mean and what are you referring to?

So I've been banging my head on this for a while. In some ways, it feels like I'm getting close, but in other ways, it seems far.

According to Koa:

app.callback()
Return a callback function suitable for the http.createServer() method to handle a request. You may also use this callback function to mount your koa app in a Connect/Express app.

So, doing ss.http.middleware.prepend(app.callback()); actually turns Koa routes on to start working. The problem is, it turns off ss routes!

I also gave koa-connect (see link below) a try, but that seems to only add middleware to koa, not the other way around (adding koa to ss).

I'm pretty tired right now, so I'll have to hack on this another time. Just going to post a few resources I've been reading through so I can return to them (and to help anyone else who wants to take a crack at it):

https://github.com/koajs/koa/blob/master/docs/guide.md
http://www.jongleberry.com/why-you-should-and-shouldnt-use-koa.html
http://stackoverflow.com/questions/25385382/using-express-middleware-in-koa
https://github.com/vkurchatkin/koa-connect/blob/master/index.js
http://dailyjs.com/2014/01/09/koa/

What I meant was that I don't understand the prepend/append notation for the middleware. If it is a middleware/router it should just slot in as a single entry in the app, and you can just add things before and after it. I think it is from a time where the connect/express use config was a lot less flexible.

It seems to me that Socketstream is taking on the concern of configuring URLs for the application in general rather than just the ones for the defined views.

Ah, I see, I see. I like this more slimmed down idea, yes!

Got it working...more explanation in a bit. Still not sure it's 100% right, but it's definitely working...

Update: harumph. It's not serving static ss assets, so doing something wrong.

2nd update: I think it's working now, using a library called koa-connect—more soon—

So, here's the PR of work. #2

I'll make some comments and reference them in this thread to explain what I'm basically doing. (Open to criticism—feel free to comment on that pull request.)

Helper library: https://github.com/socketstream/ss-examples/pull/2/files#r28963344

Mounting the middleware: https://github.com/socketstream/ss-examples/pull/2/files#r28963430

Starting Koa and SocketStream: https://github.com/socketstream/ss-examples/pull/2/files#r28963550

Example Koa middleware: https://github.com/socketstream/ss-examples/pull/2/files#r28963660

Example SS client, served via Koa: https://github.com/socketstream/ss-examples/pull/2/files#r28963904

Example Koa route (using koa-router module): https://github.com/socketstream/ss-examples/pull/2/files#r28963964

Going to close this issue, but would be happy to have any comments/insights from others.