Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sending data to client using socket in node.js
In this case, we assume that we are using emit/send function, not createReadStream.
What I want is to send data to the client via emit/send function every time the for loop runs once.

What I have tried:

JavaScript
server:
socket.on('getData', function(data){
   for( let i = 0, ; i < data['leng']; i++ ){
       socket.send('data', i); // or emit
       console.log('server '+data);
   }
});

client:
socket.emit('getData', {leng: '100'});
socket.on('message', function(data){
    console.log('client '+data);
});


I want like this result
server 1
client 1
server 2
client 2
server 3
client 3
server 4
client 4
server 5
client 5
....

but result is:
server 1
server 2
server 3
server 4
server 5
....
server 100

client 1
client 2
client 3
client 4
....
client 100


The client waits for the server to finish looping before receiving data.
Is there any way to send data to the client each time it is executed inside the for loop?
Posted
Updated 22-Feb-23 22:37pm
v2
Comments
Richard Deeming 23-Feb-23 4:18am    
Have you tried wiring up the socket.on('message', ...) handler before calling socket.emit('getData', ...)?
Chopin2001 23-Feb-23 8:08am    
yes.
client:
socket.on('message', function(message){
console.log(message);
}
Richard Deeming 23-Feb-23 8:38am    
But did you move that before the socket.emit('getData', {leng: '100'}); line?

I don't know about Node, but regular JavaScript is mostly single-threaded. I suspect the socket.emit call isn't returning until the socket.on('getData', ...) handler has finished, by which time all of the console.log("server", ...) calls have already run.
Chopin2001 23-Feb-23 8:53am    
It doesn't seem to be a send/message issue.
The code inside the for loop seems to be causing the problem.
The actual code is below.
socket.on('binary', function(data){
	if(data['pID'] == s_num ){
	    for( let i = -1, max = page_limit['count']; i < max; i++ ){
           const imgData = getDataStream(data_header, s_num);
			socket.send(imgData)
			data_header = undefined;		   
        }
    }
});

//get imageheader information
function getDataStream(_data, _num){
    if( _data !== undefined && _data.page == '1' ){
	return readBinary(_data, _num);	
    }else{
	return readBinary(napiFile[_num].getNext(_num), _num); 
    }
}
 
function readBinary(_data, _id){
    var fd = fs.openSync(getPath(_id), 'r');
    if(fd){	
	   var bkgndBuffer = new Buffer.alloc(_data.size);     
	   fs.readSync(fd, bkgndBuffer, 0, _data.size, _data.address);   
	   _data.src = bkgndBuffer.toString('base64');
    }else{
	   return false;
    }
    fs.close(fd);

    return _data;
}
//client
socket.emit('binary', {pID: id});
socket.on('message', function(data){
   MYAPP.Mediator.loadStream(data);
})
Richard Deeming 23-Feb-23 8:58am    
So as I keep saying, what happens if you move the socket.on('message', async function(data) { ... }); line BEFORE the socket.emit('binary', {pID: id}); line?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900