I'm wondering if someone could explain the differences between the Haskell websocket libraries and how they work with Yesod.
The two main examples I'm going off are this tutorial for Network.Websockets and the wai websockets package.
I haven't been able to find a good example or tutorial for the wai package. Since yesod uses wai, what advantages does it have over the simple network one? Is it meant to be used instead of, or inconjunction with Network.Websockets?
I've seen examples, but I haven't been able to understand how they actually fit into the yesod environment. If somebody could explain how websockets relate to handlers and IO and such in Yesod, that would be great.
Note: I have seen this question and this question but I can't understand how to apply them, as they don't actually explain where websockets fit into Yesod.
Yesod websockets are now available. "cabal install yesod-websockets" fetches and installs the package. You can read about it at on Michael Snoyman's March 9, 2014 blog at http://www.yesodweb.com/blog/2014/03/wai-yesod-websockets.
The fact is that websockets fall outside the scope of normal request/response pairs. As such, normal Yesod handlers are not able to simply start using websockets. That would be ideal, and perhaps we'll add that support in the future, but I haven't figured out a way to do it yet (ideas/suggestions/discussion are highly welcome!).
In the meanwhile, the way to integrate websockets is to drop down a layer of abstraction and use wai-websockets directly with Warp. The answers you link to describe how to get that done.
I'm not sure what your requirements are, but eventsource fits into the existing request/response paradigm much better, and the yesod-eventsource package makes it easy to integrate with Yesod. So if that fulfills your requirements, you may want to check it out. (Eventsource also has the advantage that it works correctly with HTTP reverse proxying.)
UPDATE This answer is no longer accurate; please see David's answer for updated information.
Related
i am looking for a very lightweight framework that can very simply pass data between browser and the server. i started looking around for a possible alternative to express & socket.io and came across express.io and have a couple of questions:
1) when i look at express.io on github, it would appear that there has been no activity for a couple of years. should this be of any cause for concern? from my newbie perspective, i would think that one wants to strike a balance between stability and something that might not be under active development anymore. but i am only guessing.
2) websockets looked very interesting and i see some very recent activity there. any thoughts on if this is a good replacement for socket.io ? our application will only be running on a new html5 browser.
i have looked at quite a few possibilities (layers, kaph, locomotive, stapes, hapi, etc) but i wanted to stick closely with something that was more sinatra-like, very similar to express. i also thought perhaps i should look for recent development.
or could it be that there is really nothing better than the original socket.io & express ?
thank you very much.
socket.io is built on top of webSockets. It adds a bunch of useful features that webSockets do not have such as automatic reconnection, structured message passing, etc...
So, one would only really go back to webSockets from socket.io if you had no use for any of the extra features that socket.io adds or if you need to be compatible with some other endpoint that is only a webSocket and not socket.io.
As for express.io, here's one such discussion of a similar question: What utility of express.io VS express + socket.io?. The fact that there has been no development on it in a few years and that the domain it was using has expired seems like it is no longer being supported unless there is some other fork that is being developed. I don't know express.io very well myself, but it appears to be just be some extra glue that allows you to capture socket.io messages using express-like route definitions. It doesn't appear to really offer new capabilities that can't be done with just express and socket.io.
For you to get much else in helpful advice, I think you'd have to describe a lot more about your requirements and why express + socket.io doesn't already meet your needs.
https://pusher.com/ is a really nice software as a service that accomplishes this functionality. It's similar to socket.io and also runs on webSockets. There are a lot of other services like that out there. Pusher's free tier is not too bad, and you likely won't run over the hard constraints unless you're making enough money to pay for the premium tier of service. Their libraries are also super easy to understand.
socket.io is still a great choice, just wanted to show you that there are more options out there.
I'm looking to build a REST API using Node and Express and I'd like to provide documentation with it. I don't want to craft this by hand and it appears that there are solutions available in the forms of Swagger, RAML and Api Blueprint/Apiary.
What I'd really like is to have the documentation auto-generate from the API code as is possible in .NET land with Swashbuckle or the Microsoft provided solution but they're made possible by strong typing and reflection.
For the JS world it seems like the correct option is to use the Swagger/RAML/Api Blueprint markup to define the API and then generate the documentation and scaffold the server from that. The former seems straightforward but I'm less sure about the latter. What I've seen of the server code generation for all of these options seem very limited. There needs to be some way to separate the auto-generated code from the manual code so that the definition can be updated easily and I've seen no sign or discussion on that. It doesn't seem like an insurmountable problem (I'm much more familiar with .NET than JS so I could easily be missing something) and there is mention of this issue and solutions being worked on in a previous Stack Overflow question from over a year ago.
Can anyone tell me if I'm missing/misunderstanding anything and if any solution for the above problem exists?
the initial version of swagger-node-express did just this--you would define some metadata from the routes, models, etc., and the documentation would auto-generate from it. Given how dynamic javascript is, this became a bit cumbersome for many to use, as it required you to keep the metadata up-to-date against the models in a somewhat decoupled manner.
Fast forward and the latest swagger-node project takes an alternative approach which can be considered in-line with "generating documentation from code" in a sense. In this project (and swagger-inflector for java, and connexion for python) take the approach that the swagger specification is the DSL for the api, and the routing logic is handled by what is defined in the swagger document. From there, you simply implement the controllers.
If you treat the swagger specification "like code" then this is a very efficient way to go--the documentation can literally never be out of date, since it is used to construct all routes, validate all input variables, and connect the API to your business layer.
While true code generation, such as what is available from the swagger-codegen project can be extremely effective, it does require some clever integration with your code after you initially construct the server. That consideration is completely removed from the workflow with the three projects above.
I hope this is helpful!
My experience with APIs and dynamic languages is that the accent is on verification instead of code generation.
For example, if using a compiled language I generate artifacts from the API spec and use that to enforce correctness. Round tripping is supported via the generation of interfaces instead of concrete classes.
With a dynamic language, the spec is used at test time to guarantee that both all the defined API is test covered and that the responses are conform to the spec (I tend to not validate requests because of Postel's law, but it is possible too).
First of all, I have seen this question: How to best create a RESTful API in Node.js and it has pointed me towards mers, which has been a great help.
But I have also been reading a lot of good things about derbyjs and it does look really interesting.
So my questions, does it make sense to use derbyjs for ceating a REST API (real-time features might be useful in the future, but not a 100% certain at this pont.)? And is it any better or worse than mers?
I am really grateful for any help.
Edit:
If anyone is interested, decided now to use sails.js: http://sailsjs.org/
The strength of Derby is that the same views (i.e. rendering templates into HTML) can be executed on the client as well as on the server. So for building a webapp, you won't have to explicitly code a REST API and then use it from the client-side JavaScript, instead you just write your views and Derby does the rest.
So if you're looking into making a REST API only (as your question states) and no HTML, there is no advantage in using Derby. It's the wrong tool for the job.
It depends on what you're looking for exactly. Derby.js is built on top of Express.js which has excellent support for creating a REST API. This also means that anything you can do in Express, you could also do in Derby. If you want real-time features, and the ability to build out a REST API, Derby.js is an excellent choice. It's also one of the reasons that people recommend Derby over something like Meteor (currently Meteor does not have support for REST endpoints, but it will hopefully in the future so also something you might want to keep your eye on, if you're in the market for real-time framework). However, if you're not looking for a node framework with an emphasis on real-time functionality, Derby is not the right choice. I would however recommend looking into Express.js to build a REST API. We use it currently for that purpose and it works really well. There are also a number of libraries and packages that play nicely with Express, so in the future if your needs change, it's easy to find something that works well with Express.
Anyway, I would recommend checking out some basic tutorials for how to create a REST API in Express because once you're able to successfully do that, adding some of the real-time features of Derby.js is fairly straightforward.
Basic tutorial on creating a REST API in Express.
http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/
I started developing an application (some boring accounting software for inhouse use) and decided to make it web-based as this would solve a couple of problems in one go.
Now with a couple of webframeworks for Haskell to choose from (happstack, yesod and snap) I still begin feeling the pain again of having to deal with HTML, CSS and devishly clever wired JavaScript.
Uhm.
So what the hell I thought might as well try going all the AJAX way and do the UI in either Cappuccino or SproutCore. (SEO is not an issue here).
But now I wouldn't really need a full blown web-framework such as one of the three above, an HTTP server which could serve data wrapped in JSON or XML should in theory be enough.
Would there now still be a point in using either one of those three?
And most of all how feasible is the approach?
Günther
I think your question might be verging on "overly broad" here - so I guess it depends on your requirements. You probably don't need things like type-safe URLs if all you are doing is exchanging JSON and in fact the Yesod book has a JSON web service example which avoids using Yesod itself and instead builds on the underlying WAI package.
I can't comment on Snap and Happstack since I haven't really looked into them much, but there are other options too. For example Scotty adds a simpler layer on top of WAI which should be more than adequate for building a basic server without much of a learning curve. I also found reading Scotty's code was a good way to build an understanding of WAI which is also very useful if you are developing more complicated Yesod apps.
All of the big three web frameworks have their own web servers. Yesod's server is warp. Snap's server is snap-server. Happstack's server is happstack-server. They all have fairly low level APIs that would be appropriate for your application. Warp's API is defined in wai. The Snap server's API is defined in snap-core. Happstack doesn't have a separate package for its API, however they have a simplified version of it in the happstack-lite package.
I would recommend that you take a look at the APIs and use whichever one you like best.
I want to work with WebSockets in Node.js web app, and I am looking for the easiest way to do this. I've seen so many github repositories seemingly providing some ease of use.
But, I'm just looking to see if there's one that stands out as having the most support, or most widely implemented.
I was kind of leaning towards Socket.IO but I'm not entirely sure.
Any advice?
Thanks!
use now now or socket.io.
now is an abstraction build on socket.io which allows you to define methods on a shared object across client and server. This means you dont have to interact with the stream manually and can just seemingly call methods. Do read their best practices before use though.
now also has a grouping system in build which means you can talk to clients in groups rather then one or all.
socket.io itself is recommended because of it's excellent browser support with its range of fallbacks. It's also owned/maintained by a node.js startup so it's more likely to be maintained in the future. And it also has a range of server-side socket.io implementations for platforms other then node.js so you can use the same API on multiple platforms.
If you find socket.IO too large or bloated you can go for the lightweight websocket-server. This is just a simple websocket implementation and is reasonably stable. I have personally used this if I want something which is a very minimal abstraction and if I want more low level access to the websocket server itself.
Take a look at this blog post, it's very informative...