{"id":"a8313866d5a82416","repo":"websockets/ws","slug":"the-data-size-must-not-be-greater-than-125-bytes","errorCode":null,"errorMessage":"The data size must not be greater than 125 bytes","messagePattern":"The data size must not be greater than 125 bytes","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"lib/sender.js","lineNumber":256,"sourceCode":"   */\n  ping(data, mask, cb) {\n    let byteLength;\n    let readOnly;\n\n    if (typeof data === 'string') {\n      byteLength = Buffer.byteLength(data);\n      readOnly = false;\n    } else if (isBlob(data)) {\n      byteLength = data.size;\n      readOnly = false;\n    } else {\n      data = toBuffer(data);\n      byteLength = data.length;\n      readOnly = toBuffer.readOnly;\n    }\n\n    if (byteLength > 125) {\n      throw new RangeError('The data size must not be greater than 125 bytes');\n    }\n\n    const options = {\n      [kByteLength]: byteLength,\n      fin: true,\n      generateMask: this._generateMask,\n      mask,\n      maskBuffer: this._maskBuffer,\n      opcode: 0x09,\n      readOnly,\n      rsv1: false\n    };\n\n    if (isBlob(data)) {\n      if (this._state !== DEFAULT) {\n        this.enqueue([this.getBlobData, data, false, options, cb]);\n      } else {\n        this.getBlobData(data, false, options, cb);","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/websockets/ws/blob/ae1de54330cef77e487548890fabfeb9aae1d83d/lib/sender.js#L238-L274","documentation":"Thrown by Sender.ping() when the ping payload exceeds 125 bytes. Ping is a WebSocket control frame and RFC 6455 section 5.5 mandates control-frame payloads MUST NOT exceed 125 bytes and MUST NOT be fragmented. The guard is at lib/sender.js:255-257 after computing byteLength from the string/blob/buffer input.","triggerScenarios":"Calling ws.ping(data, mask, cb) on an OPEN connection where data (string, Blob, or ArrayBuffer/Buffer) has byte length > 125. The check runs after Sender.ping() normalizes the input to a byteLength and throws RangeError when byteLength > 125.","commonSituations":"Using ping as a heartbeat with large timestamps or serialized state; sending binary diagnostics in pings; copying example code that put large buffers in pings; misinterpreting ping as a general data channel.","solutions":["Keep ping payloads to small heartbeat tokens (e.g. a timestamp or counter) under 125 bytes.","Move large data to ws.send() instead of ws.ping().","If measuring liveness, send an empty ping (ws.ping()) and rely on the pong event, not the payload."],"exampleFix":"// before\nws.ping(Buffer.alloc(200));\n\n// after\nws.ping(Buffer.alloc(8)); // <= 125 bytes","handlingStrategy":"validation","validationCode":"function pingSafe(ws, data, mask, cb) {\n  const len =\n    typeof data === 'string'\n      ? Buffer.byteLength(data)\n      : data && data.byteLength != null\n        ? data.byteLength\n        : 0;\n  if (len > 125) {\n    const err = new RangeError('ping payload > 125 bytes');\n    if (cb) process.nextTick(cb, err);\n    return;\n  }\n  ws.ping(data, mask, cb);\n}","typeGuard":"function isSmallControlPayload(data) {\n  if (data == null) return true;\n  const len = typeof data === 'string' ? Buffer.byteLength(data) : data.byteLength;\n  return len != null && len <= 125;\n}","tryCatchPattern":"try {\n  ws.ping(data, mask, cb);\n} catch (err) {\n  if (err instanceof RangeError && /must not be greater than 125 bytes/.test(err.message)) {\n    // drop oversized ping or shrink payload\n  } else {\n    throw err;\n  }\n}","preventionTips":["Reserve ping payloads for tiny heartbeats (timestamp/counter).","Send large content via ws.send() data frames, not control frames.","Validate payload size before calling ping() to avoid a synchronous throw."],"tags":["websocket","control-frame","rfc-6455","input-validation"],"analyzedSha":"ae1de54330cef77e487548890fabfeb9aae1d83d","analyzedAt":"2026-08-03T19:11:18.437Z","schemaVersion":2}