You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use websocat as an npm module for tighter integration with node apps. Automatically downloads websocat.
Usage
npm install websocat
constwebsocat=require("websocat")// Web service to proxy (on client)constwebService=micro((req,res)=>res.end("Hello world!"))webService.listen(3000)// Start websocat that ingests a tcp/http request and hosts a websocketconsthttpInWebSocketOut=awaitwebsocat.create({listen: "tcp-l:127.0.0.1:3001",host: "ws://127.0.0.1:3002",exitOnEOF: true,binary: true,})// Start a websocat that ingests messages from a websocket and forwards// to our hello world serverconstwebsocketIn=awaitwebsocat.create({listen: "ws-l:127.0.0.1:3002",host: "tcp:127.0.0.1:5000",exitOnEOF: true,binary: true,})constresponse=awaitrequest(`https://127.0.0.1:2000`)// response === "Hello world!"awaithttpInWebSocketOut.stop()awaitwebsocketIn.stop()
Options
awaitwebsocat.create({// Alias: listenaddr1: "tcp-l:127.0.0.1:3000",// Alias: hostaddr2: "ws://127.0.0.1:4000",// Close a data transfer direction if the other one reached EOFexitOnEOF: false,// strict line/message mode: drop too long messages instead of// splitting them, drop incomplete lines.strict: false,// Use \0 instead of \n for linemodenullTerminated: false,// Serve only once. Not to be confused with -1 (--one-message)oneshot: false,// Send and/or receive only one message. Use with --no-close and/or -u/-U.oneMessage: false,// Send message to WebSockets as binary messagesbinary: false,// Don't send Close message to websocket on EOFnoClose: false,// Send message to WebSockets as text messagestext: false,// Accept invalid certificates and hostnames while connecting to TLSinsecure: false,// Inhibit copying data in one directionunidirectional: false,// Inhibit copying data in the other direction (or maybe in// both directions if combined with -u)unidirectionalReverse: false,// Add custom HTTP headers to websocket client request.headers: {Authorization: "Bearer SomeToken",},// Add custom HTTP header to websocket upgrade reply.serverHeaders: {Authorization: "Bearer SomeToken",},})
About
Node bindings to websocat, easily create websocket/tcp tunnels