is there any way to send image to client by socket.io? - socket.io

I want to make a web app where you can send images in real-time. is there any way of doing it??
any video link or documentation link will be very helpful.
Thank You

As the official website says, socket.io can handle blob objects.
Starting in 1.0, it's possible to send any blob back and forth: image, audio, video.
For example, you can push documents from socket.io server-side like the following. Pushing from client-side to server-side is almost the same with this.
Server-side
const filePath = "./img.jpg"
const imgBinary = fs.readFileSync(filePath)
socket.emit("img",imgBinary)
Client-side
socket.on("img", imgBinary => {
const blob = new Blob([imgBinary])
const imgBlobUrl = window.URL.createObjectURL(blob)
// Insert imgBlobUrl into img.src or something
});

Related

How to get text from an image placed in a cell using Google Apps Script

I want to get text from an image placed in a cell using Google Apps Script.
What code do I write in Google Apps Script?enter image description here
Can the cell be saved as an image and the text read?
I believe your goal is as follows.
You have a Google Spreadsheet where the images are put into the cells. In this case, the images are directly put into the cells without URLs and the built-in function. The images show some texts.
You want to retrieve the images and retrieve the texts from the images.
Unfortunately, in the current stage, there are no built-in methods for directly retrieving the images in the cells. So, in this case, I use this workaround for retrieving the images from the cells. And, in order to convert the texts from the image, Drive API is used.
When these are reflected in a sample script, it becomes as follows.
Usage:
1. Install a Google Apps Script library.
In this workaround, DocsServiceApp (Author: me) of a Google Apps Script library is used. Please install this library. You can see how to install it at here.
2. Sample script.
Please copy and paste the following script to the script editor of Spreadsheet. And, please set your sheet name.
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const spreadsheetId = ss.getId();
const ar = DocsServiceApp.openBySpreadsheetId(spreadsheetId).getSheetByName(sheetName).getImages();
const res = ar.map(({ range, image }) => {
const tempId = Drive.Files.insert({ mimeType: MimeType.GOOGLE_DOCS, title: "temp" }, image.blob).id;
const text = DocumentApp.openById(tempId).getBody().getText();
Drive.Files.remove(tempId);
return { cell: range.a1Notation, text: text.trim() };
});
console.log(res);
}
In this script, the images in the cells are retrieved using a library, and the images are converted to Google Documents. And, the text is retrieved from Google Documents.
3. Enable Drive API.
Please enable Drive API at Advanced Google services. Ref
4. Testing.
When the above script is run for your provided Spreadsheet, the following result can be seen in the log.
[
{"cell":"A1","text":"098765431 ZYX"},
{"cell":"A2","text":"1234567890 abcdef"},
{"cell":"A3","text":"098765431 ZYX"},
{"cell":"A4","text":"1234567890 abcdef"},
{"cell":"A5","text":"1234567890 abcdef"},
{"cell":"A6","text":"098765431 ZYX"},
{"cell":"A7","text":"098765431 ZYX"}
]
Note:
In this answer, I propose a simple method. When your sample Spreadsheet is used, the processing time was about 30 seconds. If your actual Spreadsheet has a lot of images, it might be required to split the cells and use the script. Please be careful about this.
References:
DocsServiceApp (Author: me)
Files: insert
Files: delete

Not able to get the current content in blocks of editorState in draft.js in mobile device

const blocks = convertToRaw(editorState.getCurrentContent()).blocks;
I have noticed that my content is not coming in the blocks in mobile. I want to know the reason.
const blocks = convertToRaw(editorState.getCurrentContent()).blocks;
I have tried that in onChange function. But not bale to get the content but it working fine on web.

ExpressJS: Retrieving images from MongoDB and displaying them in Jade

I am looking for good ways to retrieve images from MongoDB and display them. For example, to retrieve a profile picture and display it on a profile page, this is how I am currently doing it:
profile page
block content
h1= session.user.name
img(src='/avatars')
routes
exports.avatars = (req, res) ->
users.find req.session.user, (err, user) ->
if user
res.writeHead('200', {'Content-Type': 'image/png'})
res.end(user.avatar.data, 'binary')
else
res.locals.flash = err
res.render('index', {title: 'Home'})
Basically I render the profile page, and the img tag sends a request back to the server to pick up the picture of the user from the database. Im saving the images as BinData in MongoDB. Not using GridFS because I expect the pictures to be small.
Is there a better way to do this?? I tried to send the image data directly to the jade view using res.render('users/show', {picture: data}) but the img tag didn't like that and I'm not clear on why. Is this approach a dead end?
Any comments/suggestions would be much appreciated. Thanks!
Sending the image data directly to jade view would work if
a) you base64 encoded the image data
b) prefixed with "data:"
But I would not recommend you do this way, you essentially in-lining the image in your html.
You should instead create a separate route to handle your images/avatars.
For example the image source for your avatars should be something like
img(src='http://mydomain.com/avatars/user1.jpg')
When the browser requests this image you then respond with image data retrieved from mongo.

MIME Type Warning Causing Broken Image on Website

Here is a sample page having an issue:
http://estorkdelivery.com/template/view/69
Our website serves up a template preview. Once you enter text information and tab out of a field, the website serves up an updated preview of the image with the text added to the field.
When the server returns this image:
http://estorkdelivery.com/file/preview/verify_token:149505eb811f8856a12ec6e71e2932f082a97edf
It shows up broken with the following message in my console:
Resource interpreted as Image but transferred with MIME type text/html: http://estorkdelivery.com/file/preview/verify_token:149505eb811f8856a12ec6e71e2932f082a97edf
Trying to cover all bases, I've verified the server has the following MIME types set:
application/x-javascript .js
image/jpeg jpeg jpg jpe
image/png png
I'm not sure why this is happening. I was wondering if someone could help me troubleshoot. Any ideas?
Thanks!
Update 04/27/2012 - My question was marked down, but I have done research on this issue. If this question doesn't merit an answer, can someone at least point me in the direction I need to go in order to continue troubleshooting? I don't ask questions lightly and I have read plenty of similar issues on StackOverflow to find myself still with the same problem. It's discouraging to be marked down without a polite explanation. Thanks.
Honestly, I don't think it's a server setup problem; when your AJAX request comes back with a verify_token, I can use that to get an image from your server. Then, using JavaScript, I can get the new preview to display on the page.
This section looks odd:
new $._iframeUpload(form, {
success: function(response)
{
$('.preview').attr('src', 'http://estorkdelivery.com/file/preview/verify_token:'
+ response.verify_token);
var image = new Image();
image.src = 'http://estorkdelivery.com/file/preview/verify_token:'
+ response.verify_token;
image.onload = function()
{
$queue--;
if ($queue <= 0)
$('.updating').hide();
};
}
});
I don't really understand why an Image object is being created after editing the img tag. I would change your JavaScript to look like this (so that the onload callback works):
new $._iframeUpload(form, {
success: function(response)
{
var image = new Image();
image.onload = function()
{
$('.preview').attr('src', image.src);
$queue--;
if ($queue <= 0)
$('.updating').hide();
};
image.src = 'http://estorkdelivery.com/file/preview/verify_token:'
+ response.verify_token;
}
});
Try that and tell me if it helps.
Minor quirk
This isn't really in answer to your question, but it influences how this whole image loading process works. Your preview is 700 x 500, but the new previews that you're returning from your server are 2900 x 2100! That's making your AJAX load time much longer than your initial page load. You might consider sizing it down on the server side and then transfering it to the browser.

How can I return binary image data from an abortable AJAX request and set the result to the src of an HTML/DOM image?

I'm writing a web application that involves a continuous cycle of creating (and removing) a fair number of images on a webpage. Each image is dynamically generated by the server.
var img = document.createElement("img");
img.src = "http://mydomain.com/myImageServer?param=blah";
In certain cases, some of these images outlive their usefulness before they've finished downloading. At that point, I remove them from the DOM.
The problem is that the browser continues to download those images even after they've been removed from the DOM. That creates a bottleneck, since I have new images waiting to be downloaded, but they have to wait for the old unneeded images to finish downloading first.
I would like to abort those unneeded image downloads. The obvious solution seems to be to request the binary image data via AJAX (since AJAX requests can be aborted), and set the img.src once the download is complete:
// Code sample uses jQuery, but jQuery is not a necessity
var img = document.createElement("img");
var xhr = $.ajax({
url: "http://mydomain.com/myImageServer?param=blah",
context: img,
success: ImageLoadedCallback
});
function ImageLoadedCallback(data)
{
this.src = data;
}
function DoSomethingElse()
{
if (condition)
xhr.abort();
}
But the problem is that this line does not work the way I had hoped:
this.src = data;
I've searched high and low. Is there no way to set an image source to binary image data sent via AJAX?
You would have to base64-encode the data into a data: URI to achieve that. But it wouldn't work in IE6-7, and there are limitations on how much data you can put in there, especially on IE8. It might be worth doing as an optimisation for browsers where it's supported, but I wouldn't rely on it.
Another possible approach is to use the XMLHttpRequest to preload the image, then just discard the response and set the src of a new Image to point to the same address. The image should be loaded from the browser's cache. However, in the case where you're generating the images dynamically you would need to pay some attention to your caching headers to ensure the response is cachable.
Try e.g.
this.src="data:image/png;base64,XXX"
...where XXX is your binary data, base64-encoded. Adjust the content-type if necessary. I wouldn't be optimistic about wide browser support for this, though.
You should be able to use data URIs, similar to the solution I identified in an earlier question. Note that this will not work with older browsers.

Resources