question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TCP vs WebSocket isn't actually "translated"

See original GitHub issue

The transit relay doesn’t actually un-frame incoming TCP messages and turn them into properly-sized WebSocket messages. (Related: the only transit-over-websocket client has a “complimentary” bug whereby it turns the WebSocket stream back into a byte-stream).

The transit should act differently when either side is a different transport protocol. If both sides are the same (i.e. TCP<–>TCP or WebSocket<–>WebSocket), messages can be directly transmitted.

If just one side is TCP, the incoming stream must be buffered until at least one message is available (according to the “first 4 bytes are the size” framing). Then this message can be sent to the WebSocket side as a single binary WebSocket message (without the 4 byte size).

In the other case, incoming WebSocket messages should have their length prefixed before being streamed to the TCP side. Thus the maximum size of any incoming WebSocket message can be at most 2**32 - 4 (which we can enforce at the WebSocket layer via Autobahn options).

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
meejahcommented, Oct 1, 2022

I think the correct fix here is to split the “relaying” state into two: “translating” or “relaying” (the latter used when both sides are the same, the former used when they’re different).

For “relaying” we continue to just funnel bytes (or messages) back and forth.

In “translating” we must buffer until a complete message is received. When bytes come in on WebSocket connections, this is easy (we have “one message” always). On incoming TCP we must buffer.

Having two states allows the outputs to correspond to the correct behavior.

1reaction
meejahcommented, Oct 3, 2022

Yeah … also worth bearing in mind that Twisted itself is our intermediary between the kernel and dataReceived() so there may be multiple “on the wire” TCP packet payloads in a single dataReceived() call, I believe.

So, yeah, there could be more than 1 complete message, there could be part of one, the finish of the one we want plus some more, etc. This is why my instincts are that “buffering” (until there’s at least one complete message) is going to be the easiest implementation, not frames.

(I do expect all compliant websocket libraries to deal with incoming websocket frames since they can be arbitrarily split up … but have we confirmed this for the clients we care about?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

TCP Socket to Websocket? - Stack Overflow
WebSockets is a message based transport while TCP is a lower-level streaming transport. The TCP transport layer does not have a concept of ......
Read more >
WebSockets - A Conceptual Deep Dive - Ably Realtime
The WebSocket protocol created new possibilities for communication over the Internet and opened the door to a truly realtime web.
Read more >
Differences between WebSockets and Socket.IO - ITNEXT
When talking about WebSockets, we refer to a web communication protocol that provides a full-duplex communication channel over a single TCP ...
Read more >
Writing a WebSocket server in C# - Web APIs | MDN
WebSockets communicate over a TCP (Transmission Control Protocol) connection. Luckily, C# has a TcpListener class which does as the name ...
Read more >
The WebSocket protocol - IETF Datatracker
The WebSocket message does not necessarily correspond to a ... Conceptually, WebSocket is really just a layer on top of TCP that adds...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found