Socket Connection Timeout

Jagdish
Jagdish Member Posts: 2
edited August 23 in Developer APIs

i have written various code to upload image through cloudinary but in all code i'm getting socket connection timeout. i'm using multer where i'm using memoryStorage can anyone provide me a correct way of code or tell me the reason for socket timeout even when most thing are correct in the code like my network and cloudinary config

this is my multer code -:

const multer= require('multer');
const storage = multer.memoryStorage();
const fileFilter=(req,file,cb) =>{   

if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png' || file.mimetype === 'image/gif')

{       

cb(null,true);   

}

else

{       

cb(new Error("Invalid file type. Only JPEG, PNG, and GIF files are allowed"), false);   

}

}
const upload=multer({    storage,    limits:{        fileSize : 1024 * 1024 * 1    },     fileFilter: fileFilter})
module.exports= upload this is my cloudinary code

const cloudinary=require('cloudinary').v2;
cloudinary.config(

{   

cloud_name:"***********",   

api_key:"************",   

api_secret:"*****************",

});
const uploadOnCloudinary = async (buffer) => {   

try {       

if (!buffer) {           

console.log("No buffer provided");            return null;        }       

return new Promise((resolve, reject) => {            const uploadStream = cloudinary.uploader.upload_stream({                resource_type: "auto",                timeout: 30000            }, (error, result) => {                if (error) {                    console.error("Upload error:", error); // Log only the message                    return reject(new Error(`Upload failed,${error.message}`));                }                resolve(result);            });            uploadStream.end(buffer);        });   

}

catch (error) {       

console.error("Upload error:", error);       

}
module.exports={    uploadOnCloudinary}

my route

router.post('/add/warehouse',verifyToken.verify,verifyRole('WAREHOUSE'),upload.fields([{name:"wareHouseImage", maxCount:5}]),warehouseController.AddWareHouse);

and my controller

    if (req.files && req.files.wareHouseImage && req.files.wareHouseImage.length > 0) {       

await Promise.all(req.files.wareHouseImage.map(async (file) => {
        const dataUriString = DataURIParser.format(path.extname(file.originalname).toString(), file.buffer);
                uploadResult = await uploadOnCloudinary(dataUriString.content);                                                    if (uploadResult && uploadResult.url) {              imageURL.push(uploadResult.url);                }
              })          );      } else {          return next(ApiErrors(400, "No file uploaded. Please upload some pictures"));        }

Please review this code and give me some hint        


Answers

  • Tom
    Tom Member, Cloudinary Staff Posts: 113

    Hi @Jagdish ,

    Thanks for reaching out.

    The socket timeout message typically suggests an issue with your application's network connectivity or a third-party factor like a firewall, security system, VPN, or another system that may be blocking your application's access to the internet.

    Unfortunately, it is hard to debug these types of errors as your request never makes it to our servers.

    Having said that, other Cloudinary users have faced similar issues and upgrading the node.js version can help. See https://stackoverflow.com/questions/76179568/socket-connection-timout-error-in-node-js .

    Let us know if the above helps at all.

    Thanks,

    Thomas