I am trying to display filesize on with socket.io with every time file changes. Here is my js code.
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
var fs = require("fs");
var filesize;
var filename = 'D:\\test.txt';
fs.watchFile(filename, function() {
fs.open(filename, 'r', function(err, fd) {
fs.stat(filename, function(err, stat) {
if(err) { console.log('error'); }
console.log(stat.size);
filesize = stat.size;
});
})
});
io.on('connection', function(socket){
console.log('A user connected');
socket.emit('testerEvent', { description: prntarr});
socket.on('clientEvent', function(data){
console.log(data);
});
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
http.listen(3000, function(){
console.log('listening on localhost:3000');
});
The problem is , socket is not refreshing data but console shows changed size. It refreshes only when i reload the page. Please suggest where i am making mistake and how to fix it.
thanks in advance.
You need to do a socket.emit() inside your fs.watchFile() callback. You're not emitting anything when the file changes at the moment
When a user connects, push his socket in an Array.
Inside the watchFile callback, iterate through the connected sockets Array and .emit() what you want to send.
Don't forget to remove from that
Array any sockets that have disconnected
Related
I'm trying to build a simple app using Node, Express (^4.15.3), and socket.io (^2.0.3). I'm building a simple chat app, however each time I add a new message, I get an additional response each time.
For example, if the first message was "Hello", I would be returned:
Hello
If I then add a subsequent message of "Is anyone there?", I get back:
Is anyone there?
Is anyone there?
And so on...each time I get an additional response back.
Here's my code - it feels like it's something really obvious and I may have been staring at it too long...
//app.js
var express = require("express");
var bodyParser = require("body-parser");
var expressValidator = require('express-validator');
var session = require('express-session');
var passport = require("passport");
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.use(express.static("public"));
app.set("view engine", "ejs");
server.listen(process.env.PORT || 3000, process.env.IP, function(){
console.log("Server starting...");
});
app.get('/testsocket', function(req, res){
res.render('sockets/test');
});
io.sockets.on('connection', function(socket){
console.log('Connected')
socket.on('send message', function(data){
console.log('Got the message...');
io.sockets.emit('new message', data)
});
});
And then client side
//client side
$(function() {
var socket = io.connect();
var $messageForm = $('#send-message');
var $messageBox = $('#message');
var $chat = $('#chat');
$messageForm.submit(function(e){
e.preventDefault();
socket.emit('send message', $messageBox.val());
$messageBox.val('');
socket.on('new message', function(data){
$chat.append(data + "<br>");
});
});
});
I think the problem is that you're registering the listener for new message inside your submit function. So each time you submit the form you register a new listener, in addition to any previous listeners. Try putting the socket.on('new message', ... section outside the submit handler.
I have this code working for receiving data from my Arduino but I will like to send data back to my Arduino and get a response on my client page. I added a listening function but I keep getting io.on is not a function when I send data from my client page.
test.js
io.listen(app.listen(3000)).on('connection', function (client) {
// store client into array
clients.push(client);
// on disconnect
client.on('disconnect', function() {
// remove client from array
clients.splice(clients.indexOf(client), 1);
});
// I added this to listen for event from my chart.JS
io.on('connection', function(socket){
socket.on('LED on', function (data) {
console.log(data);
});
socket.on('LED off', function (data) {
console.log(data);
});
});
});
Your value of io is not what it should be.
The usual way of doing things is like this:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(80);
io.on('connect', ...);
But I'm guessing that your value of io is something like this:
var io = require('socket.io');
That's not the same thing. That's the module handle. But, when you do it this way:
var io = require('socket.io')(app);
Then, io is a socket.io instance. You can bind listeners to an instance, not to the module handle.
In every single socket.io server-side example on this doc page, they use one of these forms:
var io = require('socket.io')(app);
var io = require('socket.io')(port);
var io = require('socket.io')(server);
with this:
io.on('connection', ....);
Nowhere do they do:
var io = require('socket.io`);
io.listen(server);
io.on('connection', ....);
That's just the wrong value for io.
Long story, shortened, you need to fix what you assign to io to be consistent with the docs. It's the return value from require('socket.io')(app); that gives you a socket.io instance object that you can then set up event handlers on.
if you are using express
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
let APP_PORT=3000;
server.listen(APP_PORT,()=>{
console.log(`SERVER RUNNING ON PORT : ${APP_PORT}`);
});
io.on('connection', (socket) => {
/* SOCKET - CORE EVENTS */
socket.on('connect', (message) => {
console.log("connected: " + message+"socket_id:"+socket.id);
});
socket.on('disconnect',(data)=>{
console.log('user disconnected:' + socket.id);
});
socket.on('error', function (err){
console.log('received error from client:', socket.id,' Error :',err);
});
});
I am trying to create a basic messaging system using express/nodejs. While I am able to emit messages to all users successfully. I need for users to be able to message each other in a 1-to-1 private manner.
What I am trying to do below is simple. When a user logs in, once the session has been validated, store the user object in the clients array and make this array accessible in the view -- that's it!
The intention is that this array will to grow on the server as users log in and I'll need to make it accessibly in the view, so I can generate a list of online users that are available for chat.
I have tried several different approaches, the approach below results in an empty array in the view.
My goal is to simply store online users in an array on the server as they log in and have that array accessible in the view.
I appreciate any suggestions.
index.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');
var session = require('client-sessions');
var server = require('http').createServer( app );
var io = require('socket.io').listen( server );
var clients = [];
app.locals.delimiters = '<% %>';
app.get('/', function(req, res){
if(req.session && req.session.user.username){
mysql.query("SELECT * FROM users WHERE username = ? AND pass = ? LIMIT 1", [req.session.user.username, req.session.user.pass], function(error, results, fields){
if(results.length === 0){
req.session.reset();
res.redirect('/login');
}else{
res.locals.user = results[0];
io.sockets.on('connection', function(socket){
clients.push({user : res.locals.user, socket : socket});
});
res.render('index');
}
});
}else{
res.redirect('/login');
}
});
server.listen( 3331 ); //chat port
io.sockets.on('connection', function(socket){
//load all users
socket.on('load users', function(){
io.emit('load users', {clients : clients});
});
socket.on('error', function(err){
console.error(err.stack);
});
});
module.exports = app;
index.hjs
<div class="messenger-user" ng-repeat="n in clients">
<div class="avatar-icon glyphicon glyphicon-user">
</div>
<div class="user-meta">
{{n.user.firstname}} {{n.user.lastname}}
</div>
</div>
index.hjs JavaScript
socket.emit('load users', function(clients){
$scope.clients = clients;
$scope.$apply();
});
Makes perfect sense. It sends an empty array because you send it before populating it.
Currently :
1) A MySQL query is launched to retrieve and populate the users array.
2) Meanwhile, a socket on 'connection' event is triggered (line 24), that emits the users array, which is currently empty.
3) Your MySQL query ends (line 3) and gets back with the data you want.
4) Inside the callback function, you set another listener for io.sockets.on('connection'). It's too late, because you already set one, and the connection event already happened. So you have two listeners for the same event, and the second one never triggers.
What you should do is wait for the SQL query to end, then populate your array and emit it to the client.
Edit 2 : Don't use both HTTP connection and socket for authentication. You're trying to store a user object from a HTTP request, together with a socket that is unrelated to the HTTP request. Do everything with the sockets :
var clients = [];
io.sockets.on('connection', function(socket){
socket.on('credentials', function(data){ // data == { "username":"John" , "password":"123456" }
login(data, socket); // sending credentials and socket to the login function, so both are defined there.
})
socket.on('error', function(err){
console.error(err.stack);
});
});
function login(data, socket){
mysql.query("SELECT * FROM users WHERE username='"+data.username+"' AND pass='"+data.password+"' LIMIT 1", [req.session.user.username, req.session.user.pass], function(error, results, fields){
if(!results.length){
req.session.reset();
res.redirect('/login');
}else{
clients.push({user : results[0], socket : socket}); // Both defined!
io.emit('load users', {clients : clients});
res.render('index');
}
});
}
server.listen( 3331 ); //chat port
I really hope to find some answers here as i tried everything by now.
Background:
Overtime we deploy code to web server, we need to do a cache warm up, i.e. access the site and make sure it loads. First load is always the slowest since IIS require to do some manipulations with a new code and cache it.
Task:
Create a page which will a checkbox and a button. Once button is pressed, array of links sent to server. Server visits each link and provides a feedback on time it took to load each page to the user.
Solution:
I am using node JS & express JS on server side. So far i manage to POST array to the server with links, but since i have limited experience with node JS, i can not figure out server side code to work.
Here is a code i got so far (it is bits and pieces, but it gives an idea of my progress). Any help will be greatly appreciated!!!
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var parseUrlencoded = bodyParser.urlencoded({ extended: false});
var http = require("http");
function siteToPrime(url){
http.get(url, function (http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
});
});
};
//Tells express where to look for static content
app.use(express.static('public'));
app.post('/', parseUrlencoded, function(request, response){
var newBlock = request.body;
console.log(Object.keys(newBlock).length);
var key = Object.keys(newBlock)[0];
console.log(newBlock[key]);
siteToPrime("www.google.com");
response.status(201);
});
app.listen(3000, function(){
console.log("Server listening on port 3000...");
});
Assuming that you have access to the array in the post route:
var express = require("express"),
request = require("request"),
app = express();
var start = new Date();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: false}));
function siteToPrime(req, res, urls) {
urls.forEach(function(url)) {
request(url, function(error, res, body) {
if (!error && res.statusCode == 200) {
console.log(url +' : ' + body);
console.log('Request took: ', new Date() - start, 'ms');
}
});
}
res.redirect('/');
};
app.post('/', function(req, res){
var urls = req.body.urls // Array os urls.
siteToPrime(req, res, urls);
});
app.listen(3000, function(){
console.log("Server listening on port 3000...");
});
i'm having problems in removing users in Socket.IO, upon their browser close the user in the array users[] is not removed and not updated and i would like also to update the users in the client side. Can you help me remove user programmatically? Here is my code guys, i hope you can help me with my code provided below. I've already surfed the net on how to remove users but i can't really make it work. Thanks in advance guys.
Here is my server.js:
var redis = require('redis');
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var publish = redis.createClient();
var subscribe = redis.createClient();
server.listen(8080);
var users = [];
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.on('adduser', function (user) {
users[user] = user;
console.log(users);
});
socket.on('disconnect', function (user) {
delete users[user] = user;
io.sockets.emit('update', user);
// delete users[user] = users;
// socket.emit('update', users);
console.log(users)
});
socket.on('update', function () {
users[user] = user;
console.log('Current users: ', users);
});
});
Here is my client.html:
<html>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('connect', function(){
socket.emit('adduser', prompt("What's your name?"));
});
</script>
<div style="float:left;width:150px;border-right:2px solid black;height:510px;padding:10px;overflow:scroll-y;text-align:center;">
<b>Users</b>
<div id="users"></div>
</div>
</html>
Thanks again guys, i hope you can help me :)
The server-side disconnect event isn't going to have a user variable in its callback, so you are effectively using delete users[null]. Instead, you should be storing the user variable in the socket-specific data store, like so:
var io = require('socket.io').listen();
var users = [];
io.sockets.on('connection', function(socket) {
socket.on('adduser', function(user) {
socket.set('username', user, function() {
users[user] = user;
});
});
socket.on('disconnect', function() {
socket.get('username', function(err, user) {
delete users[user];
io.sockets.emit('update', users);
});
});
});
When the socket disconnects, you can then find its associated user variable, and then delete it.