2

I want to use socket.setKeepAlive() in my application.

This is an example of using this function:

var net  = require('net');
var server = net.createServer(function(socket){
    socket.setKeepAlive(true,60000);

And another proper way to use this function:

var socket = net.connect(opts, function(){
    // 'connect' listener
    socket.setKeepAlive(true, 5000);
    socket.write("hello");
});

Since all this options use vanilla Node.js, how can i use this function with the express framework?

In express, i do not include the net module. Instead, i use this:

var express    = require('express');
var app        = express();

1 Answer 1

1

Based on this article:

https://github.com/expressjs/express/issues/3556

You could try using this express middleware:

var express = require('express')
var app = express()

app.use(function (req, res, next) {
  req.socket.setKeepAlive()
  next()
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Do you know if i can use it inside an async function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.