I'm thinking about how limiting it is for AJAX apps to have to poll for updates, when what would be ideal is for javascript to be able to set up a real two way connection to the server. I'm wondering if there is some method of integrating javascript with a browser plugin that can make a tcp connection so that I could pass data into and out of the browser plugin.
WebSockets is designed to solve this problem.
Here is an implementation with a similar approach:
socketjs
It uses a Java Applet and bridges its API to JavaScript, interesting...
And here another one:
jSocket
This one is a wrapper of the Actionscript 3 Socket API, bridged to JavaScript...
You can use node.js framework's socket.io package which can can be installed via npm (A node package manager).
More detailed usage.
jSocket and Stream are two options that utilize Flash's built-in XML sockets, though neither appears to be production-ready. I'd lean towards using a Flash-based solution rather than Java, as browser penetration is higher and generally offers a better user experience (load times & stability).
Related
I seem to be confused by conflicting sources, yesterday I was reading the node docs and was sure Node's 'net' and 'http' modules had web socket capabilities, but maybe I misunderstood the documentation because today an article said that node has no built in web socket support.
Can you create a node server that can handle web socket connections with just node and javascript, no external libraries?
Node does not have native support for websockets like it does for http or tcp (net) connections.
It's been discussed a few times, and rejected for various reasons of the last few years. The current discussion is going on here: https://github.com/nodejs/node/issues/19308
You can, of course, implement a websocket server yourself using the native modules, but you'll need to do a lot of boilerplate work.
To see both an example of what you'd need to do to implement your own websocket server using node and a good pre-built library you can use to work with websockets in node, I would reccomend taking a look at: https://github.com/websockets/ws
Sure, you could... if you re-implemented the functionality from those Web Socket libraries yourself.
The Node.js core libraries are minimal. They're only intended to cover the basics needed to function, and the most common use cases. As much functionality as possible is left to modules outside of the Node.js core. This is a very intentional design.
It would be incredibly irresponsible to implement your own Web Socket library without a very good reason. You should rethink why you don't want to use existing libraries.
I currently have a Qt-based GUI application that simply provides a graphical dashboard (guages, graphs, and such) displaying real-time data provided by another application via a TCP connection. For architectural and security reasons, I have been told that my TCP connection is no longer allowed, and that an HTTPS websocket will be my only conduit. It was suggested that I convert my app to a webapp using HTML5 and Javascript. However, I'm not a web programmer, but may consider this an opportunity to learn a new technology. I want to understand the landscape a little better before making a decision. So, I think my choices are:
Convert my app to a web app, giving me all the advantages/disadvantages of web-based apps. More work for me, as it's a new technology and I already have the Qt/TCP version working fine. If this is the suggested approach, any suggestions on a development environment/tools would be appreciated.
Convert my current Qt app to us a client HTTPS connection rather than a TCP connection. Not sure this is possible. From what I've read, this may not be possible with QtWebKit. Seems strange?
Maybe there's another choice I'm not considering?
You can also use a mixed approach, using WT library. It's c++ (and QT inspired) based.
It has a nice toolkit and supports websockets.
http://www.webtoolkit.eu/wt/examples/
Porting requires a lot of work. On Qt side there are at least two implementations of WebSockets:
QtWebSocket, Qt-based websocket server implementation and alpha-state implementation of websocket client.
WebSocket++, Boost-based websocket server and client implementation.
I just went through a similar exercise for work recently. We settled on the Google Web Toolkit (GWT) framework for doing our web apps. It's completely java-based, meaning you write (almost) everything in Java and the GWT compiler converts it to Javascript for you. We used the GWT-EventService plugin for pushing data from the server to the clients.
I wish I had know about this WT library before we started though, that looks interesting.
You can also have a look at QWebSockets, which is a pure Qt implementation of websockets, both for client and server use.
Update: this library is now an integral part of Qt
I am working on app where I need to pass messages between a C++ application and a Javascript web app.
Certainly I could write sockets code myself in either language and I have done this in the past when necessary.
What I would really like is a higher-level message posting or message queueing API that does a lot of the work for me. Does anyone know of such an API?
I have looked at ICE, and it doesn't appear to have Javascript bindings. I have also looked at Boost message queue, but it only caters for the C++ side of things. If necessary I might roll my own Javascript bindings for either of these technologies.
UPDATE: Sorry should have mentioned this before, I want to run this in a browser.
To give a more complete story what I want is a simple browser-based app that is used to configure and display logging for a C++ application.
I know there are other ways of doing this, but I am specifically interested in a high-level library in both C++ and browser-based Javascript that builds a message queue ontop of the sockets API (if there isn't one then I might consider implementing it myself and writing up a code project article).
ALSO: I'm not bothered about portability in terms of the web browser. Eg if there is a high-level IPC Javascript library that only works in Chrome, I'll be happy with that.
With JavaScript I assume that you are running it in a browser? In this case your C++ application needs to provide a webserver and some kind of JSON based webservice that you can call. On the JavaScript side you just use AJAX to communicate with that webservice.
An alternative would be websockets which might be a little harder to implement on the C++ side though.
To simply answer your question: No, there is no IPC implemented in ECMAscript out of the box.
But you actually answered you question already. If you try to communicate with Javascript that runs in a browser, you indeed should use (web-)sockets connections to pipe date in either direction. Of course you could write a simple HTTP server in C++, but I guess that is overkill and does not have the capabilitys of bi-directional sockets.
It's still some work to implement a web-socket connection in C++ from the scratch (the specs were in flux for a long time), but I guess there are some librarys out already.
If you're trying to communicate with node.js, this is an almost trivial task using real sockets/pipes.
I have found a solution that meets my needs. It isn't exactly perfect but I think it works well enough.
Some people suggested using HTTP and ajax. That turned out to be a useful idea and after some prototyping I think it solves my rather basic needs.
To be more specific I am using the Mongoose HTTP server embedded in my C++ application and I am using the jQuery ajax function to pull data from the server. The jQuery client polls the server continously for new data, not particularly efficient but I think it will do the job good enough for me.
Once my implementation is complete I'll write an article explaining how to do this in detail and then I'll update this answer.
You could try DBus, it has very simple mechanism to define, query and use interfaces, and there are some components for XPCOM and webkit based browsers (for example http://sandbox.movial.com/wiki/index.php/Browser_DBus_Bridge and http://code.google.com/p/v8-dbus/). Also DBus is opensource and cross platform.
For a server side or non-browser implementation how about named pipes?
Yes it's vintage technology and the usage depends which OS you use, but as long as your server side js environment has ability to read and write files it may work, and it fits the description 'high-level' inter-process communication.
I want to have sort of live blogging that will allow a reporter to live blog and add updates on-the-fly.
What is the best way to do this? Check for updates every X seconds or use some kind of AJAX push?
Some examples:
http://www.bbc.co.uk/news/world-middle-east-12307698
http://thelede.blogs.nytimes.com/2011/03/11/video-of-the-earthquake-and-tsunami-in-japan/
I would definitely look at using WebSockets or some sort of push technology.
It depends on your level but you can either use a third party service like Pusher, use an open source project like Juggernaut or build your own.
If you're going to bake a solution yourself look at Node.js, EventMachine and Socket.IO. You'll also need some sort of capable pub/sub queuing system. Redis or RabbitMQ are perfect.
I feel Node.js and JavaScript are more suited to building a realtime push server but if you are more comfortable with Ruby EventMachine is also great.
I'd use Socket.IO for all solutions. Socket.IO basically uses WebSockets if your browser/mobile supports them and falls back to Flash, polling etc. It takes all the headache away involved with browsers and their lack of support for HTML5 features.
Personally I would use Juggernaut as that combines Socket.IO, Redis and Node.js which are the technologies I'd choose to build such a system. Juggernaut is readymade, straightforward to setup and easily scalable thanks to Node.js and Redis.
Would it be possible to write a basic SSH client in pure javascript using something like orbited web sockets as the transport layer? I don't see any examples of this, but it seems like it would be a nice thing to have.
You can check out Ajaxterm and rTerm, which uses it.
I really like Orbited and used it on one of my company's internal systems (with RabbitMQ). It should be possible to take the concepts from those applications and use Orbited as the transport layer, though I don't think this is the best use case for it (as the task being single-user and not channel-based etc.).
With a proxy on the server to convert the unpack the WebSocket protocol and tunnel it into SSH, sure, it could be done.
It hasn't been done yet because WebSocket isn't really finished yet and most browsers don't have implementations. You have to fall back to Flash sockets to emulate it.