Comments (7)
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.
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.
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.
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.
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.
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 viareadable.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.
I'm closing this as answered.
from ws.
Related Issues (20)
- TypeError when extending WebSocket in typescript HOT 3
- WS-Server using express.js leads to "invalid frame header" when connecting with browser HOT 6
- How to get client ip address from wss.clients Set after connection event? HOT 7
- Switchable server side PONG HOT 12
- options.autoPong HOT 4
- Error creating WebSocket: TypeError: HQ.WebSocket is not a constructor (Typescript) HOT 5
- The connection has no header "User-Agent", so that I have stucking in the readyState 0. HOT 2
- Detecting broken connections fails with `RSV1 must be clear` on node v20.11.0 HOT 14
- Sec-WebSocket-Accept not found HOT 7
- ws issues with custom hostname HOT 2
- WebSocketServer.address() error needs more context HOT 2
- clientTracking - the client is not destroyed if the server closes or terminates the connection. HOT 2
- The code isn't working. HOT 14
- Is there anyway to disable the websocket closeTimeout? HOT 9
- Unhappy TypeScript when using compilerOptions: module: Node16 || NodeNext HOT 1
- Support sending Blob HOT 13
- WebSocket Ping-Pong Timeout and Connection Closure Failure HOT 3
- terminate() doesnt terminate instantly HOT 6
- ws doesn't work with sveltekit's adapter-cloudflare HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ws.