ERR_SOCKET_DGRAM_IS_CONNECTED
Node.jsERRORNotableNetworkHIGH confidence

dgram socket is already connected

Production Risk

Low — call disconnect() before reconnecting a UDP socket.

What this means

Thrown when dgram.connect() is called on a UDP socket that is already in a connected state. A connected UDP socket has a fixed default peer; calling connect() again without first calling disconnect() is not permitted.

Why it happens
  1. 1Calling socket.connect() twice on the same dgram socket
  2. 2Calling addMembership() on a connected socket (some operations require unconnected sockets)
How to reproduce

Triggered when connect() is called on a dgram socket that has already been connected.

trigger — this will error
trigger — this will error
const dgram = require('dgram');
const s = dgram.createSocket('udp4');
s.connect(41234, 'localhost', () => {
  s.connect(41235, 'localhost'); // throws — already connected
});

expected output

Error [ERR_SOCKET_DGRAM_IS_CONNECTED]: Already connected

Fix

Call disconnect() before connecting to a new peer

WHEN When switching the connected peer

Call disconnect() before connecting to a new peer
s.disconnect();
s.connect(41235, 'localhost', () => {
  // now connected to new peer
});

Why this works

disconnect() releases the current peer association, allowing a new connect().

Code examples
Triggerjs
const dgram = require('dgram');
const s = dgram.createSocket('udp4');
s.connect(41234, 'localhost', () => {
  s.connect(41235, 'localhost'); // throws — already connected
});  // this triggers ERR_SOCKET_DGRAM_IS_CONNECTED
Handle in try/catchjs
try {
  // operation that may throw ERR_SOCKET_DGRAM_IS_CONNECTED
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_SOCKET_DGRAM_IS_CONNECTED') {
    console.error('ERR_SOCKET_DGRAM_IS_CONNECTED:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_socket_dgram_is_connected(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Call connect() twice without disconnecting

A connected dgram socket has a fixed peer; the second connect throws.

Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors