I'm trying to setup some socket.io communications, the communication between my server (app.js)(runs on a raspberry pi) and a website(public/index.html) works fine. Now I want to expand it so when my app.js receives a call from index.html it emits it further to another node.js script(bed.js) that will run on another raspberry pi. I tried to use the npm module socket.io-client, but this can only receive apparently
!edit! problem has narrowed down to the setrgb part, there it won't emit.
!edit 2! when i receive setRGB, i emit setRGBclient, but that can only be received in bed.js, not in index.html, there lays my problem, i need to share the connections or force it to another connection, no clue how i fix it though
APP.JS:
let http = require('http').createServer(handler); //require http server, and create server with function handler()
let fs = require('fs'); //require filesystem module
let io = require('socket.io')(http) //require socket.io module and pass the http object (server)
let delay = require('delay');
console.log('Define each color from RGB Strip light.');
http.listen(8080); //listen to port 8080
function handler (req, res) { //create server
fs.readFile(__dirname + '/public/index.html', function(err, data) { //read file index.html in public folder
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'}); //display 404 on error
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'}); //write HTML
res.write(data); //write data from index.html
return res.end();
});
}
io.sockets.on('connection', function (socket) {// WebSocket Connection
socket.on("test", function(){
console.log("sampletext");
});
socket.on("setRGB", function(data){
socket.emit("setRGBClient", data);
console.log(data);
console.log("test");
});
});
bed.js:
let socket = require('socket.io-client')('http://localhost:8080');
let lightstate = false;
let stayOff = false;
let fadeState = false;
console.log("check");
socket.emit("test");
socket.on("setRGBClient" ,function(data) {
console.log(data);
});
I can just broadcast setRGBClient.
socket.broadcast.emit("setRGBClient", data);
I guess this is a learning exercise. Otherwise I’d caution against socket.io for such applications.
However I can only see the subscription for ‘setRGB’ not the emit-part.
Related
my friend and I are both using the same wifi network with open ports for both of us. My friend is listening on port 8000 and I'd like to send him a message.
How can I achieve that using node and socket.io ? Thanks a lot
There are many ways to setup a simple messenger, the standard one is to setup a single socket.io server either on your or your friend's machine, something like this:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(8000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
console.log("user connected!")
socket.on('message', function (message) {
//Sends message to all connected sockets
socket.broadcast.emit("message")
});
});
Then your index.html should have some code for connecting to server and sending messages:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://server-ip-or-domain-name:port');
socket.on('message', function (message) {
//processing incoming message here
console.log(message);
});
function sendMessage(msg){
//sends message to server
socket.emit("message", msg);
}
</script>
In this setup both you and your friend should be able to reach the server machine. Use pings for debuging.
p.s. havent tested the code above, but that's the idea.
Currently, I have a lot of linux based clients downloading firmware updates from my webserver.
After the client has successfully downloaded the firmware file, my server needs to execute a few scripts, which logs in to the client and performs some tasks.
Is there a way for a node server to keep track of the clients download progress, so I can execute the needed scripts once the file has been downloaded?
Ok so I will try.
If you serve your Firmware as static files through Apache/Nginx and direct url call. You don't get the progress inside your NodeJS script.
If you serve your files via stream inside a express controller you can listen to the progress. Look at this answer here https://stackoverflow.com/a/42273080/3168392
You will have to use a socket connection to make sure the node server gets update from the client of the progress of the file being downloaded.
Something like this
CLIENT_CODE
var socket = io('http://localhost');
socket.on('connect', function(){});
socket.on('data_reciving', parts.pop(),function(percentage){
if(parse_data(percentage) === 100){
client.socket.emit('downloadCompleted', {fileName:'test'});
}else{
//do nothing
}
});
SERVER_CODE:
sockets.on('connection', function (socket) {
//listen to the event from client
socket.on('downloadCompleted', function (data) {
connect_to_client();
do_some_operation();
socket.emit('ALLDONE',{some_data});
});
});
I guess this helps ,you can use this post for reference.
If you just want to run some code when a download has finished, you can use on-finished:
const onFinished = require('on-finished');
app.use((req, res, next) => {
onFinished(res, (err, res) => {
...log some data, perform some housekeeping, etc...
});
next();
});
As is, it will attach a "finished" listener to all responses, which is probably not what you want. Since this is plain Express middleware, you can attach it to specific routes instead (but how depends on how exactly the files are being served).
I found some code that seems to fit my needs.
With the code below, I can detect both the progress of a user's download from the server-side, and fire an event once the file transfer completes.
var http = require("http");
var fs = require("fs");
var filename = "./large-file";
var serv = http.createServer(function (req, res) {
var sent = 0;
var lastChunkSize = 0;
var stat = fs.statSync(filename);
res.setHeader('Content-disposition', 'attachment; filename=large-file.iso');
res.setHeader('Accept-Ranges', 'bytes');
res.setHeader('Keep-Alive', 'timeout=5, max=100');
res.writeHeader(200, {"Content-Length": stat.size});
var fReadStream = fs.createReadStream(filename, { highWaterMark: 128 * 1024 });
fReadStream.on('data', function (chunk) {
if(!res.write(chunk)){
fReadStream.pause();
lastChunkSize = chunk.length;
console.log('Sent', sent, 'of', stat.size);
}
});
fReadStream.on('end', function () {
console.log('Transfer complete.');
res.end();
});
res.on("drain", function () {
sent += lastChunkSize;
fReadStream.resume();
});
});
serv.listen(3001);
Tried different methods, but the data is sent to a maximum of one or two clients. How to send data to all the clients connected to the server ? What am I doing wrong?
Server.js:
var PORT = 3000;
var options = {
// 'log level': 0
};
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server, options);
server.listen(PORT);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/attantions/templates/.default/template.php');
});
io.sockets.on('connection', function (client) {
client.on('attantion', function (data) {
try {
// Tried so
io.sockets.volatile.emit('attantion', data);
// And tried so
io.sockets.emit('attantion', data);
client.emit('attantion', data);
client.broadcast.emit('attantion', data );
} catch (e) {
console.log(e);
client.disconnect();
}
});
});
Client.js:
socket.emit("attantion", data);
socket.on('attantion', function (data) {
pushData(data);
});
See this post for different options for socket.io messages
Send response to all clients except sender (Socket.io)
io.sockets.on('connection', function (client) {
client.on('attantion', function (data) {
//client.emit('attantion', data ); // This will send it to only the client
//client.broadcast.emit('attantion', data); // This will send it to everyone but this client
io.emit('attantion', data); // This will send it to all attached sockets.
});
});
Edit
I wonder if this post can help you?
Socket.io - Cannot load file
I was curious how sending the php file to the client through node.js works? are you using another framework?
Could you show more of what your client code looks like? loading the lib and the instantiation of the socket.
I have a node.js server communicating to a client web page, sending it message. This is working great based on the many tutorials and searching stack overflow :)
Where I am having an issue is when I attempt to startup a separate socket connection to a 3rd party cmd line executable instance runs as a socket server. The 3rd party executable does not adhere to the socket.io namespace/room type of events, so I read that socket.io-events may help where instead of:
socket.on('some key', function(){/**do stuff*/}); I could:
eventRouter.on('*', function(){/*do stuff*/});
For this communication, I am assuming I need to use socket.io-client on the node.js side in order to talk to the cmd executable, but I am getting exceptions trying to do a socket2.use(router); where socket2 is my socket.io-client and router is the socket.io-events object.
All runs on localhost, node to web page is port 8001 and node.js to executable is on port 8002. Please pardon the code, for I have been trying to get this to work for a few days and is a bit ugly now.
The cmd executable to execute and its arguments I have coming from the web page which works. I am able to start the exe. The EXE expects a ACK on each message sent, thus why you see the code emitting it back.
I have a interval where I set and update an element on the web page. I have another element that I set messages (msg).
var http = require('http');
var url = require('url');
var fs = require('fs');
var server;
server = http.createServer(function(req, res){
// your normal server code
var path = url.parse(req.url).pathname;
switch (path){
case '/':
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Hello! Try the Test page </h1>');
res.end();
break;
case '/socket.html':
fs.readFile(__dirname + path, function(err, data){
if (err){
return send404(res);
}
res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'});
res.write(data, 'utf8');
res.end();
});
break;
default: send404(res);
}
}),
send404 = function(res){
res.writeHead(404);
res.write('404');
res.end();
};
server.listen(8001);
var str = "ack0";
var bytes = [];
for (var i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
// use socket.io
var io = require('socket.io').listen(server);
// define interactions with client
io.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('date', {'date': new Date()});
}, 1000);
//recieve client data
socket.on('client_data', function(data){
var spawn = require('child_process').spawn;
console.log('pre-spawned');
spawn(data.cmd, data.args, {});
setTimeout(function() {
console.log('hello world!');
}, 1000);
var aptIO = require('socket.io-client');
var router = require('socket.io-events')();
var socket2 = aptIO.connect('localhost:8002', {reconnect: true});
router.on('connection', function(s){
//send data to client
console.log('apt');
router.on('*', function(sock, args, next){
var name = args.shift(), msg = args.shift();
console.log(name + " " + JSON.stringify(msg));
sock.emit(bytes);
io.sockets.emit('msg', {'msg': JSON.stringify(msg)})
next();
});
s.emit(bytes);
});
console.log('spawned');
// getting runtime exceptions here...have tried various things...
socket2.use(router);
});
});
With the help from JGreenwell, I was able to resolve me issue.
I ended up having the node server communicate to the client html page via socket.io connection for messages. The node server would launch the cmd line executable providing it the port to connect to which is different from the socket.io port used.
Once started, the executable would communicate with the server via the net module. The server would just pass the information on to the socket.io connection. the js in the html page knows how to parse the message in order to increment the progress bar and list the messages in a text area control.
I took it even further by having the messages be broadcast-ed to multiple clients on the socket.io connection.
Learning about node.js and socket.io and working through this tutorial by Daniel Nill. The server starts no problem. However, when I navigate to localhost:8001/socket.html, I get the default error message. So I changed the switch statement to be '/socket.html' as opposed to 'socket.html'. The page now loads with status code 200, but nothing is rendered to screen. The screen should say "This is our socket.html file". What gives?
The server side js code is
var http = require("http");
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
break;
case 'socket.html':
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
break;
}
response.end();
});
server.listen(8001);
Socket.html located in the same directory as server.js contains this
<html>
<head></head>
<body>This is our socket.html file</body>
</html>
Ok I gave up on this and moved over to this example which works right out of the box!
Beginner here. As far as I can tell Daniel Nill wrote a bunch of code for a tutorial, and it never worked. As a result, he just added to the confusion for beginners--something he claimed he was trying to alleviate.
So I changed the switch statement to be '/socket.html' as opposed to 'socket.html'.
That was one obvious error--good catch.
The page now loads with status code 200, but nothing is rendered to
screen. The screen should say "This is our socket.html file". What
gives?
Or, like I'm seeing, if the socket.html file doesn't exist, instead of getting a 404 error I get a status code of 200 (OK) and an empty web page.
The reason the code in the tutorial doesn't work is because Daniel Nill thought he would be clever and not write response.end() after every response.write(). He thought he could just write one response.end() at the end of all the code.
It appears to me that Daniel Nill misunderstood how nodejs works. To wit, nodejs does NOT execute a function and then wait around for the handler function passed as an argument to finish executing before executing the next lines of code. If nodejs actually did that, then we wouldn't need to put our code inside handler functions. Instead, nodejs adds a handler function to a list of handler functions that are to be executed at some time in the future.
Look at the fs.readFile() function in this code:
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
break;
case 'socket.html':
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
break;
}
response.end();
The handler function for fs.readFile() is this part:
function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
});
When a browser requests /socket.html, nodejs executes fs.readFile() and then nodejs adds its handler function to the list of handler functions awaiting execution, then nodejs continues on. The next line of code that will execute is the response.end() here:
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
break;
}
response.end(); //<====HERE ******
It's apparent to me that before the handler function from fs.readFile() has a chance to execute, nodejs executes that response.end().
According to the docs for response.end():
response.end([data], [encoding])
This method signals to the server
that all of the response headers and body have been sent; that server
should consider this message complete. The method, response.end(),
MUST be called on each response.
I tested it, and if you don't do any response.write()'s and you just call response.end(), nodejs will create an empty response with a status code of 200, for example:
switch(path) {
case '/':
//resp.writeHead(200, {'Content-Type': 'text/html'} );
//resp.write('<h3>Root page</h3>');
resp.end();
break;
I think the lesson to learn from Daniel Nill's mistake is that after you give nodejs a handler function, you have no control over where execution will take up after the handler function executes. In fact, code written after the end of a handler function can execute before the handler function executes. As a result, a handler function needs to do everything that needs to be done itself.
Here are the modifications necessary to make the example in the tutorial work correctly:
var http = require('http');
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(requ, resp) {
//requ.url => everything after the host--including the query string
//url.parse(requ.url).pathname => the portion of requ.url before the query string
var path = url.parse(requ.url).pathname;
//The following is so that the automatic request that all browsers make
//for favicon.ico (which for some reason is not logged by any developer
//tools) will not display a 'connection' message:
if (path == '/favicon.ico') {
resp.writeHead(200, {'Content-Type': 'image/x-icon'} );
resp.end();
return; //Terminate execution of this function, skipping the code below.
}
//Router:
switch(path) {
case '/':
resp.writeHead(200, {'Content-Type': 'text/html'} );
resp.write('<h3>Root page</h3>');
resp.end();
break;
case '/socket.html':
fs.readFile(__dirname + path, function(error, data) {
if (error) {
console.log('file error');
resp.writeHead(404);
resp.write("oops, this doesn't exist - 404");
resp.end();
}
else {
console.log('no file error');
resp.writeHead(200, {'Content-Type': 'text/html'} );
resp.write(data, 'utf8');
resp.end();
}
});
break;
default:
resp.writeHead(404);
resp.write("oops, this doesn't exist - 404");
resp.end();
break;
}
console.log('Connection');
});
port = 8888;
console.log('Server listening on port ' + port);
server.listen(port);
To minimize the number of times you have to call response.end(), you could do this:
var http = require('http');
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(requ, resp) {
//console.log('request url: ' + requ.url);
//requ.url => everything after the host--including the query string
//url.parse(requ.url).pathname => the portion of requ.url before the query string
var path = url.parse(requ.url).pathname;
//The following is so that the automatic request that all browsers make
//for favicon.ico (which for some reason is not logged by any developer
//tools) will not cause a 'connection' message:
if (path == '/favicon.ico') {
resp.writeHead(200, {'Content-Type': 'image/x-icon'} );
resp.end();
return;
}
//Router:
switch(path) {
case '/':
resp.writeHead(200, {'Content-Type': 'text/html'} );
resp.write('<h3>Root page</h3>');
resp.end();
break;
case '/socket.html':
fs.readFile(__dirname + path, function(error, data) {
if (error) {
console.log('file error');
resp.writeHead(404);
resp.write("oops, this doesn't exist - 404");
//resp.end();
}
else {
console.log('no file error');
resp.writeHead(200, {'Content-Type': 'text/html'} );
resp.write(data, 'utf8');
//resp.end();
}
resp.end();
});
break;
default:
resp.writeHead(404);
resp.write("oops, this doesn't exist - 404");
resp.end();
break;
}
//resp.end();
console.log('Connection');
});
port = 8888;
console.log('Server listening on port ' + port);
server.listen(port);
But you can't refactor the response.end() completely out of the handler function--like Daniel Nill did; and you can't put a response.end() after the switch statement because the response.end() will execute before the handler function passed to fs.readFile() has a chance to execute, which will cause an empty request with a status code of 200 to be sent to the browser.
Additionally, I was getting two "Connection" messages for a single request. My developer tools only showed one request being sent by my browser when I typed in a url such as:
http://localhost:8888/
...but all browsers send an additional request which retrieves /favicon.ico. You can prove that is the case by writing something like:
var server = http.createServer(function(requ, resp) {
console.log('request url: ' + requ.url);
To solve the double request problem, I added the if statement:
if (path == '/favicon.ico') {...
...which is described here:
http://tinyurl.com/odhs5le
=====
In the next part of the tutorial, in order to see the command line output when using socket.io, you have to start the server using a command like this:
$ DEBUG=socket.io:* node server.js
See the nodejs document "Upgrading from 0.9", section Log differences here:
http://socket.io/docs/migrating-from-0-9/
=====
To get the socket.io part of the code to work, I put the following in server.js:
var http = require('http');
var url = require('url');
var fs = require('fs');
var io = require('socket.io'); //New code
var server = http.createServer(function(requ, resp) {
...
...
...
});
port = 8888;
console.log('Server listening on port ' + port);
server.listen(port);
//New code:
var websockets_listener = io.listen(server);
websockets_listener.sockets.on('connection', function(socket){
socket.emit('server message', {"message": "hello world"});
});
Then in socket.html, I have this:
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div>This is our socket.html file</div>
<div id="message"></div>
<script>
var socket = io.connect();
//More recent versions of socket.io allow you to simply write:
//var socket = io();
//which both creates the socket and by default connects to
//the same host that served this page.
//See: http://socket.io/get-started/chat/, section Integrating Socket.IO
socket.on('server message', function(data) {
document.getElementById('message').innerHTML = data.message;
});
</script>
</body>
</html>
You could do this:
socket.on('server message', function(data) {
console.log(data.message);
});
...but you have to remember that in nodejs the console.log() output goes to the server window, but when javascript executes on a web page, like with socket.html, the console.log() output goes to the web browser's console (display your web browser's development tools to see the console)--so don't look for the output in the server window.
===
In the next part of the tutorial, to stream just the time, eliminating the date, milliseconds, utc offset, etc., which just clutters everything up, you can do this in server.js:
var websockets_listener = io.listen(server);
websockets_listener.sockets.on('connection', function(socket){
setInterval(function() {
var now = new Date();
socket.emit('time', {local_time: now.toLocaleTimeString()})
}, 1000);
});
socket.html:
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div>This is our socket.html file</div>
<div id="message"></div>
<script>
var socket = io.connect();
socket.on('time', function(data) {
document.getElementById('message').innerHTML = data.local_time;
});
</script>
</body>
</html>
===
In the next part of the tutorial, to stream data from the client to the server you can do this(note the corrections to the jquery, which is not up to snuff):
socket.html:
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
</head>
<body>
<div>This is our socket.html file</div>
<div id="time"></div>
<textarea id="text"></textarea>
<script>
var socket = io.connect();
socket.on('time', function(data) {
$('#time').html(data.local_time);
});
//Because the html above has already been parsed by the time this
//js executes, there is no need for document.ready():
$('#text').on('keypress', function(event) {
var keycode = event.which;
socket.emit('client data', {letter: String.fromCharCode(keycode)} );
});
</script>
</body>
</html>
server.js:
var http = require('http');
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
var server = http.createServer(function(requ, resp) {
...
...
...
});
port = 8888;
console.log('Server listening on port ' + port);
server.listen(port);
var websockets_listener = io.listen(server);
//websockets_listener.set('log level', 1);
//Rather than writing the previous line(which doesn't work anymore)
//just stop the server and restart it using this command:
//$ node server.js
//...instead of:
//$ DEBUG=socket.io:* node server.js
websockets_listener.sockets.on('connection', function(socket){
setInterval(function() {
var now = new Date();
socket.emit('time', {local_time: now.toLocaleTimeString()})
}, 1000);
socket.on('client data', function(data) {
process.stdout.write(data.letter);
});
});
Use response.sendfile instead of reading the file manually. This lets express handle the content-type for you.