I'm writing a simple streaming service.
A browser An open a web socket to a server, then another browser B open a new socket to the same server.
Browser A register a video by its camera (MediaRecorder API) and send it to the server.
The server broadcast this video to others connected browsers.
I have a working version with Socket.IO, but I'd like to do it in vanilla JavaScript (WebSocket.Server on Node.js server and webSocket on client).
This is the problem:
With socket.io a can write something like this:
this.socket.emit('broadcast', {
stream: new Blob(stream, {'type': `video/webm${MEDIA_CHARSET}`}),
from: {id: this.socket.id}
});
Stream comes from mediarecorder -> dataavailable event listener
But with native websocket I can't send blob embed in a JSON object, because websocket can send only string or arraybuffer.
I tried many different ways in order to send JSON and blob together, but nothing works.
Any help?
Is it possible to use only Engine.IO on client side in order to pack a message with blob and JSON together? Any ideas about this way?
There are many ways to serialize your data for sending over a binary web socket. I would recommend considering CBOR, which serializes to binary and also has support for binary data within it.
There are several CBOR libraries to choose from on NPM.
Related
I'm getting Server Sent Events from a particular source but, I don't want to consume them directly in a browser (Client) instead, I'd like to consume those events in a server (NodeJS) and modify those event data and store the modified data in a database.
I have tried using eventsource node module but I couldn't achieve what I wanted...
Below depicted is the flow I want to achieve.
SSE events from third party ===> NodeJS server (consume them, modify them, store them into a DB) ====> (serve to the client) Client (any browser)
Any ideas!!!
I am new to socket.io. I have a backend server implemented on nodejs which makes multiple async calls to rest api and send the response back to the client on angular 4.0. I follow the simple http request methodology. However, the issue is that it takes a while for the server to get collect and parse data from each multiple apis and send it back to the client. Therefore, I wanted to implement streaming which will return data to the client as soon as it gets from anywhere. I wanted to know if this can be done efficiently using web sockets and socket.io or is there another way around to do this.
I know, we can make async calls from the client but i want to implement the logic on the server rather than the client.
Note: I donot have realtime data
Is there a way, I could continuously send a response to the client before finally calling end on the response object?
There is a huge file and I want to read,parse and then send the analysis back to the client. Is it possible that I keep sending a response as soon as I read a line?
response.send also calls end, so I cannot use it.
You want to use res.write, which sends response body content without terminating the response:
This sends a chunk of the response body. This method may be called multiple times to provide successive parts of the body.
Note that write is not included in the Express documentation because it is not an Express-specific function, but is inherited from Node's core http.ServerResponse, which Express's response object extends from:
The res object is an enhanced version of Node's own response object and supports all built-in fields and methods.
However, working with streaming data is always little tricky (see the comment below warning about unexpected timeouts), so it may be easier to restructure your application to send streaming data via a WebSocket, or socket.io for compatibility with browsers that don't support WebSockets.
What kind of server side application are you working with? You could use Node.js with Socket.IO to do something like this. Here's some useful links.
First Node.js application
Node.js readline API
Is it possible to detect new data from the server as it is sent? For example, with express.js:
res.write('Processing 14% or something');
and then display that on the page with a progress bar.
Edit:
My original question was a bit confusing so let me explain the situation. I have created a page where users can upload song files. These files are then converted (using ffmpeg) to .ogg and .mp3 files for the web. The conversion takes a long time. Is it possible to send real time data about the conversion back to the client using the same XMLHttpRequest that sent the files?
If i understand correctly you are trying to implement event based actions. Yes node.js has got some excellent web socket libraries such as socket.io and sack.js
You need to understand nodejs event driven pattern.
Websocket protocol helps maintain full duplex connection between server and client. You can notify clients when any action happens in server and similar you can notify server when any action happens in client. Libraries provide flexibility to broadcast event to all connected client or selected ones.
So it is basically emit and on that you will be using often.
Go through the documentation, it will not take much time to learn. Let me know if you need any help.
I need to send image to client whenever it's available in the server.
The client is not a browser but some kind of device(linux box) behind NAT (3G network) that will get the image as a file, write it to disk then display it in a screen (no browser here).
I'm new to nodejs and websocket.
Can I use a nodejs as a websocket client in the device and a nodejs as a server?
Can you provide an example of using nodejs to execute a websocket client?
In this case, can nodejs detect 3G network loss then resume or restart the websocket?
What happen if connection loss happen while nodejs server is pushing an image to the device nodejs (client)? is there any resume solution?
how about Binary Js to handle file transfer between the 2 nodejs?
Can I do that with some pub/sub mecanism using a MOM like ActiveMQ?
Socket.io (which is probably what you'll want to use server-side) has a client module that works in node. It handles maintaining a connection to the server.
What you'll probably want to do is not push the image itself over the WebSocket connection. Instead, send a "new image available" notification message over the WebSocket, and when the client receives the message, make a regular HTTP GET request to the server to download the image.
Since you're making a regular HTTP request, you can use the Range header if you need to resume an interrupted download.