Search code examples
typescriptapache-kafkatcpdeno

Unable to connect to TCP socket of Kafka server


I started simple kafka server. See quick start section

I'm unable to connect to above server. This is my code:

Deno.listen({ port: 9092, transport: 'tcp' });
// or
Deno.listen({ port: 9092, hostname: 'localhost', transport: 'tcp' });
//or
Deno.listen({ port: 9092, hostname: '127.0.0.1' });

This is the error:

error: Uncaught PermissionDenied: An attempt was made to access a socket in a way forbidden by its access permissions. (os error 10013)
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
    at Object.listen ($deno$/ops/net.ts:51:10)
    at Object.listen ($deno$/net.ts:152:22)
    at Object.listen ($deno$/net_unstable.ts:36:12)
    at file:///D:/rust/deno/index.ts:10:6

Commands I've tried

deno run --allow-net --allow-write ./index.ts
deno run --allow-net --unstable ./index.ts
deno run --allow-net  ./index.ts

I'm able to connect to above server with Rust help without any problems. This is my Rust code:

fn connect() -> Result<(), std::io::Error> {
    let stream = TcpStream::connect("localhost:9092").expect("Could not connect");
}

I'm new to Deno. What I'm doing wrong?


Solution

  • With Deno.listen you're creating a server on port 9002, not connecting to the Kafka server. You should use Deno.connect instead.

    const conn = await Deno.connect({ hostname: "localhost", port: 9092 });
    console.log("Connected", conn);
    
    const decoder = new TextDecoder();
    const encoder = new TextEncoder();
    await conn.write(encoder.encode("message"));
    
    // Read response
    const buf = new Uint8Array(1024);
    await conn.read(buf);
    console.log('Client - Response:', decoder.decode(buf))
    
    
    conn.close();