socketstream / ss-examples

Examples of using Socketstream
MIT License
3 stars 7 forks source link

Combine Koa with SS #1

Closed arxpoetica closed 9 years ago

arxpoetica commented 9 years ago

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?

arxpoetica commented 9 years ago

Cross post: https://groups.google.com/forum/#!topic/socketstream/CL3gXWlv17Q

arxpoetica commented 9 years ago

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.

thepian commented 9 years ago

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 commented 9 years ago

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);
arxpoetica commented 9 years ago

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?

arxpoetica commented 9 years ago

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/

arxpoetica commented 9 years ago

(@kulicuu @paulbjensen any ideas?)

thepian commented 9 years ago

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.

arxpoetica commented 9 years ago

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

arxpoetica commented 9 years ago

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—

arxpoetica commented 9 years ago

So, here's the PR of work. https://github.com/socketstream/ss-examples/pull/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.)

arxpoetica commented 9 years ago

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.