{"record":{"id":"83e5ca327c363cfa","repo":"grafana/k6","slug":"websocket-requires-a-url","errorCode":null,"errorMessage":"WebSocket requires a url","messagePattern":"WebSocket requires a url","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/websockets.go","lineNumber":182,"sourceCode":"\t\tconnectCancel:   connectCancel,\n\t\teventListeners:  newEventListeners(),\n\t\tobj:             rt.NewObject(),\n\t\ttagsAndMeta:     params.tagsAndMeta,\n\t\tsendPings:       ping{timestamps: make(map[string]time.Time)},\n\t\tbinaryType:      blobBinaryType,\n\t}\n\n\t// Maybe have this after the goroutine below ?!?\n\tdefineWebsocket(rt, w)\n\n\tgo w.establishConnection(connectionCtx, params)\n\treturn w.obj\n}\n\n// parseURL parses the url from the first constructor calls argument or returns an error\nfunc parseURL(urlValue sobek.Value) (*url.URL, error) {\n\tif common.IsNullish(urlValue) {\n\t\treturn nil, errors.New(\"WebSocket requires a url\")\n\t}\n\n\t// TODO: throw the SyntaxError (https://websockets.spec.whatwg.org/#dom-websocket-websocket)\n\turlString := urlValue.String()\n\turl, err := url.Parse(urlString)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"WebSocket requires valid url, but got %q which resulted in %w\", urlString, err)\n\t}\n\tif url.Scheme != \"ws\" && url.Scheme != \"wss\" {\n\t\treturn nil, fmt.Errorf(\"WebSocket requires url with scheme ws or wss, but got %q\", url.Scheme)\n\t}\n\tif url.Fragment != \"\" {\n\t\treturn nil, fmt.Errorf(\"WebSocket requires no url fragment, but got %q\", url.Fragment)\n\t}\n\n\treturn url, nil\n}\n","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/websockets/websockets.go#L164-L200","documentation":"Thrown by the k6/experimental/websockets WebSocket constructor when the first argument is null, undefined, or missing. parseURL (internal/js/modules/k6/websockets/websockets.go:163) rejects nullish input with 'WebSocket requires a url' before any parsing; sibling errors cover unparsable URLs ('requires valid url') and non-ws/wss schemes.","triggerScenarios":"new WebSocket(url) where url is undefined — e.g. const url = __ENV.WS_URL with the environment variable unset, or a renamed/misspelled variable — or explicitly new WebSocket(null). An empty string instead produces the scheme error, not this one.","commonSituations":"Reading the WebSocket URL from an env var that is not set in the current k6 invocation; passing an options object as the first argument by mistake; refactoring that leaves the URL variable unassigned on some path.","solutions":["Pass the URL string directly: new WebSocket('wss://example.com/socket')","Default the env var: const url = __ENV.WS_URL || 'wss://example.com/socket'","Check typeof url === 'string' && url.length > 0 before constructing, so the failure names the missing input"],"exampleFix":"// before\nconst url = __ENV.WS_URL; // undefined when not exported\nconst ws = new WebSocket(url);\n\n// after\nconst url = __ENV.WS_URL || 'wss://example.com/socket';\nconst ws = new WebSocket(url);","handlingStrategy":"validation","validationCode":"function requireWebSocketUrl(url) {\n  if (url === null || url === undefined || url === '') {\n  throw new Error(`WebSocket requires a url — got ${String(url)}. Check __ENV.WS_URL is exported.`);\n  }\n  return url;\n}\n\nconst ws = new WebSocket(requireWebSocketUrl(__ENV.WS_URL));","typeGuard":"function isNonEmptyString(v) {\n  return typeof v === 'string' && v.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Default the URL: const url = __ENV.WS_URL || 'wss://example.com/socket'","Fail fast on missing env vars at script start rather than inside the constructor","Use ws:// or wss:// schemes to avoid the sibling scheme error"],"tags":["websocket","url","constructor","arguments","validation"],"backgroundTag":"missing-required-argument","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}