Giter VIP home page Giter VIP logo

Comments (7)

lpinca avatar lpinca commented on May 30, 2024 1

When you do fsStream.pipe(stream), every chunk of data read from the file is sent via ws.send(). The other peer receives each chunk in a separate 'message' event and the data of every message event is added to the stream readable queue via readable.push().

from ws.

lpinca avatar lpinca commented on May 30, 2024

This is expected. Every 'message' event pushes the message in the Duplex stream.

If dual triggering is expected, guidance on how to differentiate on the server side between data originating from a stream and discrete WebSocket messages.

What do you mean? It's the same data. The difference is that a chunk of data from the 'data' event might not correspond to a full WebSocket message if the stream is paused or piped to a slower destination.

from ws.

shubhams7134 avatar shubhams7134 commented on May 30, 2024

What do you mean? It's the same data. The difference is that a chunk of data from the 'data' event might not correspond to a full WebSocket message if the stream is paused or piped to a slower destination.

ClientSide Code

const client = new WebSocket('ws://localhost:9000')
const stream = createWebSocketStream(client, { encoding: 'utf8' })
const fsStream = fs.createReadStream(filePath, 'utf8')
fsStream.pipe(stream)
client.send(JSON.stringify(testInput))

In the above program, since the WebSocket-stream is piped with the fsStream, whenever we get data packets being sent to the server, both ws.on('message') and stream.on('data') get triggered. And same is the case with client.send().

So on the server side, how can we differentiate a data packet of stream and a full message?

from ws.

lpinca avatar lpinca commented on May 30, 2024

I still don't understand what you want to achieve/differentiate. The Duplex stream is a wrapper for the WebSocket so the data you get from the Duplex stream is the same data received by the WebSocket. There is no distinction.

from ws.

shubhams7134 avatar shubhams7134 commented on May 30, 2024

I'll try to explain our use case and the difficulty we are facing.

Objective:
We want the ability to stream data (large blob objects) from our client to server using websockets.
For that, we are making use of "WebSocket.createWebSocketStream" and we are trying to pipe the client's stream to this websocket stream.
The aim is to transmit this data as a stream to the server. On the server side, we're expecting another websocket stream to receive the message.

Issue:
While implementing this, both ws.on('message') and stream.on('data') are getting triggered which is causing issues because ws.on('message') receives a (incomplete) message chunk which is causing issues.

Our assumption/understanding
stream.on('data') --> This should be triggered whenever we are receiving a data chunk from a client ws stream.
ws.on('message') --> This should be triggered for normal ws.send messages from client

Query:
We want to understand if there's a way to skip hitting ws.on('message') for streams.
If ws.on('message') will get anyway get triggered, we want to understand if there's a way to distinguish between normal WebSocket (non-stream) messages and WebSocket stream messages.

from ws.

lpinca avatar lpinca commented on May 30, 2024

stream.on('data') --> This should be triggered whenever we are receiving a data chunk from a client ws stream.

That is what happens.

ws.on('message') --> This should be triggered for normal ws.send messages from client

That is also what happens.

We want to understand if there's a way to skip hitting ws.on('message') for streams.

No.

If ws.on('message') will get anyway get triggered, we want to understand if there's a way to distinguish between normal WebSocket (non-stream) messages and WebSocket stream messages.

There is no distinction. It works like this:

  • A peer calls ws.send()
  • The other peer receives a 'message' event.
  • The data received via the 'message' event is pushed to the stream readable queue via readable.push().
  • The readable side of the stream works exactly like a Readable stream.

The WebSocket protocol is message based. A WebSocket message is a chunk of data in the stream.

from ws.

lpinca avatar lpinca commented on May 30, 2024

I'm closing this as answered.

from ws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.