I am trying to set up a small system having one server (aka sensor) transfer data files to another server (aka server) (both running node.js apps) when it sees that the other is available.
The server should ideally be listening for a connection from the sensor, when connection is made, the sensor will transfer all available data files to the server, then close the connection.
I have been playing around with a library called delivery.js (https://github.com/liamks/Delivery.js), it looks promising when reading through the documentation, however, being a novice js programmer, I am struggling to understand why my attempt at the example is not working.
I am using the following code on the sensor:
var io = require('socket.io'),
dl = require('delivery'),
fs = require('fs');
var socket = io.connect('http://192.168.0.14:5001');
socket.on('connect', function() {
log( "Sockets connected" );
delivery = dl.listen( socket );
delivery.connect();
delivery.on('delivery.connect',function(delivery){
delivery.send({
name: 'sample-image.jpg',
path : './sample-image.jpg'
});
delivery.on('send.success',function(file){
console.log('File sent successfully!');
});
});
});
And the server running the example code:
var io = require('socket.io').listen(5001),
dl = require('delivery');
io.sockets.on('connection', function(socket){
var delivery = dl.listen(socket);
delivery.on('delivery.connect',function(delivery){
delivery.send({
name: 'sample-image.jpg',
path : './sample-image.jpg'
});
delivery.on('send.success',function(file){
console.log('File successfully sent to client!');
});
});
});
The server code runs fine and creates the socket listening for communication. However, the sensor code throws error:
/Users/Oliver/Desktop/socket/delivery/test-delivery-client.js:5
var socket = io.connect('http://192.168.0.14:5001');
^
TypeError: Object #<Object> has no method 'connect'
at Object.<anonymous> (/Users/Oliver/Desktop/socket/delivery/test-delivery-client.js:5:17)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:349:32)
at Function.Module._load (module.js:305:12)
at Function.Module.runMain (module.js:490:10)
at startup (node.js:123:16)
at node.js:1029:3
If anyone could explain where i am going wrong and indicate how to get my needs met, that would be great. Thanks in advance.
What you are using is just the server socket.io which can only accept incoming connections. To connect to another server you need socket.io-client package. This will give you the connect function like one you usually use in the browser, now you can use it from node.js itself.