Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bun WebSocket does not send binary data over to remote websocket server #11144

Open
kynnyhsap opened this issue May 17, 2024 · 0 comments
Open
Labels
bug Something isn't working web-api Something that relates to a standard Web API

Comments

@kynnyhsap
Copy link

kynnyhsap commented May 17, 2024

What version of Bun is running?

1.1.8+89d25807f

What platform is your computer?

Darwin 23.1.0 arm64 arm

What steps can reproduce the bug?

Simple WebScoket connection (ws.js) to a weboscket server (wss.js), sending over binary data from bun.lockb file in chunks.

// ws.js

import fs from "fs";

const URL = "https://bun-faulty-ws.up.railway.app/"; // or any remote url

const ws = new WebSocket(URL);

ws.addEventListener("open", async () => {
  console.log("connection opened");

  ws.addEventListener("error", (e) => console.error("connection error", e));

  ws.addEventListener("close", (e) =>
    console.log("connection closed:", e.reason),
  );

  ws.addEventListener("message", (e) => {
    console.log("message", e.data);
  });

  // read from file and send to wss
  const fileStream = fs.createReadStream("bun.lockb");

  for await (const chunk of fileStream) {
    console.log("sending chunk of size", chunk.length);
    ws.send(chunk);
  }
});

And here is the code for my remove weboscket server. Server is deployed (i used railway.app in this case). When running server locally it works as expected, but not if the server is on a different network.

// wss.js
import { WebSocketServer } from "ws";

const port = Number(process.env.PORT ?? 3000);

const wss = new WebSocketServer({ port });

wss.on("connection", function (socket) {
  console.log("client connected");

  socket.on("message", function (message) {
    // no message when using client connected via bun, but ok if client is node
    console.log("message from client", message.length);
  });

  socket.on("close", function () {
    console.log("client disconnected");
  });
});

console.log(`WebSocket server is running on port ${port}`);

What is the expected behavior?

binary data chunks sent over to remote websockets server

What do you see instead?

no data is sent to remote server when using bun, but ok if using node (v22)

Additional information

here is the repo with code and description of the issue https://github.com/kynnyhsap/bun-faulty-webosckets

important to say that it works when server launched locally, but not when it is a remote wss.

@kynnyhsap kynnyhsap added the bug Something isn't working label May 17, 2024
@Electroid Electroid added the web-api Something that relates to a standard Web API label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working web-api Something that relates to a standard Web API
Projects
None yet
Development

No branches or pull requests

2 participants