How to make webmachine play nice with websockets? - erlang

Webmachine seems like a great system to build REST web services, but I know it does not provide websocket compatibility. My question is rather straightforward : What would be a good approach to add websocket capabilities on top of a web service built with webmachine?

I'm running Misultin alongside Webmachine for that exact matter.

For easy use websockets in webmachine I implemented that by changing sources of webmachine (adding support mochiweb_websockets ) and mochiweb (add support secure websockets).
You can download from git full solution (webmachine with websockets support) by this link:
https://github.com/Dryymoon/webmachine.git
And by this link example usage of solution.
Or if want dig a little deeper:
I changed only two files of sources:
1) webmachine_decision_core.erl changed (add support websocket handler).
2) mochiweb_websocket.erl (add support secure websockets and patch existig).

Late answer, but just stumbled upon this, but another option is to run SimpleBridge (https://github.com/nitrogen/simple_bridge), which adds an abstraction layer (conditionally bypassed using webmachines routing system) which adds websocket support to any erlang webserver it supports (includes webmachine).

Related

Does GCDWebSever support web sockets?

Need to implement a self-host Web-Server running on iPad, looks like GCDWebServer is a great option.
However the author does not intend to support for WebSocket, see link here.
Question: Is there an alternative to GCDWebServer that support WebSocket?
End up with Swifter which is not as mature, but good enough.
See the project here.

Node.js service poller

I have an MVC web app that uses a jquery web request to generate the users notifications in a perceived async way. The notifications are built on request by each user on the site.
However I have been asked to make the notifications readily available as the happen.
This I would traditionally do using a windows service that called the same web method over http. I was thinking that this might be a good candidate functionality for using node.js
Is the any example code to call a http method in a loop and would that scale well ?
found this package and seems to do what i needed
https://npmjs.org/package/node-cron-jobs
I also came across "forever" to run node as a child process that can be re-spawned if there are errors
If you're already running on ASP.NET MVC take a look at SignalR. This was written by Microsoft, is supported by them and provides functionality similar to the Socket.IO, node.js stack.

Can a web browser use MQTT?

We are looking at using MQTT as the messaging protocol on a new device we're building. We'd also like a web interface for the device. Does anyone know if you can implement a browser client app (without additional plugins) that talks MQTT?
Yes, as mentioned in Steve-o's comment MQTT via websockets is very possible.
There are 2 options at the moment
IBM's MQ 7.5 comes with websockets support, you can find details here.
The Mosquitto broker has a javascript client with an example running here.
To answer your second question lighttpd has a websockets module that can be used to do forwarding to an existing broker with details here.
I've not been able to find anything for Apache that doesn't need you to write your own library to do the forwarding.
To extend hardillbs answer:
There is a third option now:
HiveMQ supports native websockets without the need for any external web server. It works perfectly with the Javascript client of Eclipse Paho.
If you want to try it out, the public MQTTDashboard (which uses HiveMQ under the hoods) supports websockets on port 8000.
Disclosure: I am one of the developers of HiveMQ
IBM has released a WebSockets based JavaScript client. It is open-source on Eclipse Paho.
Git repository:
http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.javascript.git/
Tutorial is here.
https://www.ibm.com/developerworks/community/blogs/c565c720-fe84-4f63-873f-607d87787327/entry/how_to_prog_javascript?lang=en
We've had good experience using WSS, the WebSocket to TCP Gateway (for MQTT). It means running a separate process, but that does avoid having to change Apache's configuration. WSS also has support for TLS.
Not Apache, but you can follow this guide to install lighttpd with mod_websocket,
http://oriolrius.cat/blog/2013/09/25/server-send-push-notifications-to-client-browser-without-polling/
but for easy of use I highly recommend HiveMQ, as #Dominik recommends.
The blog by jpmens is a good starting point to get mosquitto setup with websocket feature. He's client javascript code based on Paho is on github.
This blog "How to run your web server and MQTT WebSockets broker on the same por" explains two ways for the server side:
mosquitto with http configuration
apache+mod_websocket_mosquitto
Why not nodejs + socket.io? They come with all gadgets included (webscokets, pub/sub, webserver,mqtt broker...).
SocketCluster is another good starting point.
This link (Download codes) is useful. Download it and modify this file mosq-mqttws31.html. Instead of the embedded java script library use this cdn
https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js
and the client instantiation should be client = new Paho.MQTT.Client("test.mosquitto.org", 8080,"myclien256tiddd_" + parseInt(Math.random() * 100, 10));

What's the preferred way of using Rails and Flex?

I'd like to create a service, similar to a chat application (realtime app).
From what I can see from my research, BlazeDS is the preferred way, but it involves Java and Java EE. Also, the latest Ruby results seem to be from 2009-2010, so they are likely outdated.
Are there any actively maintained Ruby/Rails solutions for integrating push notifications with Flex?
The current situation
Do you have to use Flex? HTML5 websockets is a nascent but growing technology and there are implementations now. Flex's days are numbered. Yet, websockets doesn't yet have native support in IE.
em-websocket is a ruby websocket server based on eventmachine. The service Pusher is based on it.
Faye is a websocket server and client. (Railscast)
There are other Ruby implementations such as Cramp and Socky.
If you're open to servers in other technologies such as Node, there are many with Ruby or Javascript clients ready to go.
Update: I might mention that I looked into doing something similar with Flex a while back, and got a copy of Flex on Rails. The book's server push example uses Juggernaut, which unfortunately has stopped further development. The author states that Server-Sent Events (SSEs) make Juggernaut redundant. All major browsers except IE support them natively, similar to the situation with websockets.
There are shims ("polyfills") that use javascript to bring these missing capabilities to the browsers. For example, the jQuery Graceful WebSocket is a jQuery plugin that implements a websocket client but falls back to AJAX polling so the functionality will still work in IE, just won't be quite as instant. Because it detects websocket support, as soon as a browser supports websockets they will be used.
Bridging the Gap
We seem to be caught in a transition period, where we are at the sunset era of Flash but not yet at broad support for its replacement technologies. There is one library that may bridge the gap: Socket.IO. This library selects the most capable technology transport at runtime. It will use Flash if present, but can also use websockets, AJAX long polling, AJAX multipart streaming, a "forever iframe" if necessary. This gives it broad browser support:
IE 5.5+
Safari 3+
Google Chrome 4+
Firefox 3+
Opera 10.61+
iPhone Safari
iPad Safari
Android Webkit
WebOS Webkit
This is actually broader compatibility than either Flash/Flex or WebSockets alone. Socket.IO is implemented in Javascript for both server and client, so you need a server-side Javascript runtime such as Node.
Possible solutions
While there don't seem to be many current references to a Rails 3 -> Flex solution (as you have found), it appears there is some traction with the combination of Ruby/Rails and Socket.IO.
If you want to add chat to a Rails app using Socket.IO, there's a nice reference blog post by Liam Kaufman who creates a chat app in Rails 3 using Socket.IO: http://liamkaufman.com/blog/2012/02/25/adding_real-time_to_rails_with_socket.IO_nodejs_and_backbonejs_with_demo/
There's also a socket.io gem which adds support to the Cramp server mentioned above.
There also seem to be other stackoverflow questions with others working on the Rails 3 and Socket.IO combination.
TL;DR summary
While there isn't much indication that folks are doing direct-to-Flex from Rails anymore, there are other solutions with the most promising being a combination of Rails and Socket.IO.
If you want to live within the Ruby world, you can use regular WebSockets to talk to a Flex application. It won't be pretty, but it would work, and you could avoid the Java back-end. This would be a lot more raw than telling BlazeDS to fling structures around, but it should be doable.
On the Flex client side, there is a library written by Kaazing, that is bundled with their WebSocket servers. Download one of their WebSocket servers, and in the client-libs folder, there should be a swc (with docs) that you can use to talk to em-websocket (or really, any websocket tech).
Now, all this being said, you won't have nearly the scaleability of BlazeDS or GraniteDS, but it should work for smaller implementations and demos.
You can use https://github.com/rubyamf/rubyamf or https://github.com/victorcoder/rubyamf_plugin
But you will be have problem with realtime messaging because rubyamf and rubyamf_plugin don't support RTMP.
You can use the RestfulX gem & Flex framework. That's what I use for Rails/Flex.

What WebSockets system should I use for this?

I'm building a Rails app where I need a real time commenting system. I'm going to use WebSockets, but I'm new to them and I'm kinda lost. I tried em-websockets and websocket-rails, but neither worked well with what I have to do. I also though of a Node.JS and Socket.io app, but I don't know how to start with that.
What I want to do is send a WebSocket message when a new comment is made on a post, on the create action of my CommentsController. I'll send a message containing the comment content and creator and the post ID.
Thanks in advance! :D
Sorry, but I dont think so. Be careful with WebSockets. It is fundamental concept that provides a very powerful mechanism.
Websockets is good for super, absolutely real-time applications like online games. For commenting system (even realtime) you dont need them, the AJAX is more then enought for this.
You could use a realtime hosted service if you don't want to deal with your own realtime infrastructure, fallbacks for older browsers, scaling complications etc.
I recently wrote a post on Smashing Mag on building a realtime commenting system. It uses PHP and Pusher (who I work for) but the separation between client and server should meant that you could use any backend technology/service. It also demonstrates how to progressively enhance your app.
The most commonly used self-hosted ruby technologies for realtime communication does seem to be Faye, as #Alfred suggested.
Just using websockets as the only available transport is not a good idea, because websockets are not yet supported in every browser. Luckily for example Faye does support multiple transports so that it will work in every browser. I also found this interesting video in the past explaining how you could use Faye in conjunction with RoR from RailsCast.

Resources