flexus-net
Use the Node net
API in Chrome and Windows Apps
This module lets you use the Node.js net
module API in Chrome Packaged Apps and Windows Universal Apps.
What and why?
This is just a simple wrapper module that makes lifes easier when installing Node.js net
module with JSPM for usage in browser-like app enviroments. Namely in Chrome Packages Apps and Windows Universal Apps.
Chrome provides quirky and overly complex chrome.sockets.tcp
API for TCP socket communication which is wrapped by wonderful module chrome-net
to provide the same API as Node.js net
. The same goes for Windows and it's Windows.Networking.Sockets.StreamSocket
and StreamSocketListener
and the module winrt-net
.
And flexus-net
basically just wraps chrome-net
and winrt-net
.
And it also works with NWJS where the native Node net
is used as expected.
Installation & Usage
1) Install the module through JSPM
jspm install npm:flexus-net
2) Rename winrt-net
to net
in your SystemJS/JSPM config file
Why?
JSPM has it's own module that gets installed whenever you or your dependecy uses net
module. And it does next to nothing because browsers don't do TCP.
how?
In JSPM config file there is property map
with names and mappings of all modules. This is an example of JSPM 0.17 jspm.config.js
map: {
"flexus-net": "npm:flexus-net@2.0.0",
"events": "github:jspm/nodelibs-events@0.2.0-alpha",
"process": "github:jspm/nodelibs-process@0.2.0-alpha",
...
you change the name like so
map: {
"net": "npm:flexus-net@2.0.0",
...
and that forces JSPM to load this module whenever there is require('net')
or import net from 'net'
in your code or dependencies. Now you can write your app with net
module like you would in Node and JSPM handles picking the right module for given enviroment. When runnig in Chrome App the chrome-net
is used, in Windows App the winrt-net
is used or when running NWJS or in Node (using JSPM run path_to_your.js
) the native module is used.
Usage
Example TCP client:
import net from 'net';
var port = 22112;
var server = net.createServer(function(socket) {
console.log('connection', socket.remoteAddress + ":" + socket.remotePort);
socket.on('data', function (data) {
console.log(data.toString())
})
})
.listen(port)
.on('listening', function () {
console.log('listening')
});
Example TCP server:
import net from 'net';
var client = net.connect(22112, 'localhost')
client.write('Hello server')
client.on('data', function (data) {
console.log('received data', data.toString());
})
Or you can skip step 2) and just use import net from 'flexus-net';
See nodejs.org for full API documentation: net
JSPM & Browserify
This project was built for and tested using JSPM.
I'm not using browserify nor do I know how to set up a project for it and currently I don't have enough time to look into it now. So I don't know if this module works in browserify but I'll gladly accept any contributions towards this cause.
thanks to
Feross Aboukhadijeh, John Hiesey & Jan Schä for creating chrome-net
and Guy Bedford for JSPM