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.
Related
I'm working on a simple game, that needs to use Web Sockets. I have read that DDP should be the way to go, as it's what Meteor uses.
I can't find any documentation regarding it tho, except the note on Meteor.com.
Should I use sock js instead, or how can I use the DDP package?
DDP is a protocol that can run on top of sockets. It's not a socket library in itself. If you really need your own socket communication, then use socket.io or look for packages that do that on atmosphere. There have been some attempts made (e.g., http://arunoda.github.io/meteor-streams/), but I'm not sure what the latest is on that. This might work for you: https://github.com/joncursi/socket-io-client
Will be a support of raw sockets in node.js, e.g. to create ping packets?
A new module named node-raw-socket offers the perfect solution for real raw sockets using nodejs.
And, for creating ping (ICMP) packets, the same developer has a very nice working (using it) solution based on node-raw-sockets as well: node-net-ping.
Node supports TCP, UDP, and unix sockets. Ping packets are ICMP packets, which node cannot create directly at this time. You could execute an external ping subprocess or consider writing a C extension. Most of node's low level OS APIs are thin javascript wrappers around the corresponding C API, so you could follow that existing well-established pattern and implement this as a small JS wrapper layer around the corresponding OS-level APIs.
http://nodejs.org/docs/latest/api/all.html#all_class_net_socket
There's a chance node/javascript are a poor choice for your project based on this requirement though.
net-ping module may suit your need. to install it you can use the following command:
npm install net-ping
The documentation with examples is included here.
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'm wondering if using Node.js (express.js) is the best option to go for a complete website. I've been using Rails + Node.js and am wondering if this is still the best option or if I should move completely to Node.js.
Are there websites built completely with node.js? I imagine it must lack many things Rails offers.
I imagine it must lack many things
Rails offers.
I gave a short list below but I would like to know what you could be missing and I think(almost certain) we can give you some alternatives on node.js.
Modules
node.js is getting pretty complete and I think you can run your entire site using only node.js using for example the following modules. I assume you know about npm. If not I really advice you to google for it and learn that. To search npm you can use http://search.npmjs.org:
web framework:
High performance, high class web
development for Node.js
https://github.com/visionmedia/express
Socket.IO aims to make realtime apps possible in every browser and mobile
device, blurring the differences
between the different transport
mechanisms.
https://github.com/learnboost/socket.io-node
I believe with these two web-frameworks you can create a lot of sites. For example express is a very powerfull web framework and supports a lot of cool things like:
session support.
a lot powerful template engines. I like Jade for example. You could also share these between client and server a lot of the times easily.
excellent routing.
just to name a few.
database:
Redis is an open source, advanced
key-value store. It is often referred
to as a data structure server since
keys can contain strings, hashes,
lists, sets and sorted sets.
https://github.com/mranney/node_redis
MongoDB (from "humongous") is a scalable, high-performance, open source,
document-oriented database.
Mongoose is a MongoDB object modeling tool designed to work in an
asychronous environment.
https://github.com/learnboost/mongoose/
With those two databases I think you should be able to accomplish most of your tasks. The nice thing is that Redis is extremely fast/simple advanced KV-store(dataset in memory/also supports VM) and supports replication while Mongodb is more completely(slower then redis) and also supports sharding.
authentication:
Authentication and authorization (password, facebook, & more) for your node.js Connect and Express apps.
https://github.com/bnoguchi/everyauth
Like I said previously you can get a long way with only these modules.
Express.js is more akin to Sinatra. They're simpler frameworks than rails.
Express's list of sites is fairly small http://expressjs.com/applications.html
So I think it's also good to look at Sinatra's list http://www.sinatrarb.com/wild.html
So to answer your questions in reverse order. Yes it lacks all the features of rails. Yes there are sites written completely in Express. And going to 100% Node.js might be the right decision for your site.
It depends on which features you'd miss and what performance you need.
I'm not sure, but I guess it's perfectly possible. I have built complete sites using plain server side javascript for years without problems. The advantage of node.js seems to be its event driven model and things like socket.io. I just started experimenting with it, I'll probably will try porting an existing site to node.js.
Here you can find a large list of sites built with node.js.
Finally, you may want to read: What it’s like building a real website in Node.js
Yes, as of now node.js lacks many (well, at least a few) things rails offers. Eventually the set of available node.js modules will collectively provide good alternatives to RoR across the board. Or at least different (and often more modern) approaches to the same fundamental problems. There are still some important things missing in the node.js ecosystem including a good ORM for postgresql (rails has ActiveRecord which is great) and a good DB schema management subsystem. Both of these do not exist in the node.js ecosystem as of this moment, but surely they will be there in due time. Rails has these down pat right now.
There are some tricks node.js has that RoR doesn't. Debugging in node.js is more seamless than RoR and express.js is more flexible. Express is the library approach (you tell it what to do) whereas rails is the framework approach (you fill out the boilerplate it creates for you). There are also some fantastic things like stylus and jade, which have equivalents in rails, but when you have a full app written in one language (either javascript or CoffeeScript, which I prefer) and everything is in a modern node.js/TJ Holowaychuck style, you get a level of cohesiveness that is really nice to work within.
The other thing to keep in mind is that while the list of available node.js modules is quite impressive, many of them are not as mature and battle-tested as their rails/django analogs. It's hit or miss, so beware.
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).