1

I am Creating a chat app between two users now I can do Simple text chat with different users using node.js and socket.io. Now problem arises here as I have to send image in chat application and after searching for whole long day I am not able to get perfect node.js in which I can send image in chat app. So I want to know is it possible to send image using node.js. Here is my simple node.js file for sending simple text message from one user to another.

socket.on('privateMessage', function(data) {
    socket.get('name', function (err, name) {
        if(!err) {
            // get the user from list by its name to get its socket, 
            // then emit event privateMessage
            // again here we want to make you clear
            // that every single client connection has its own
            // unique SOcket Object, we need to get this Socket object
            // to communicate with every other client. The socket variable
            // in this scope is the client who wants to send the private 
            // message but the socket of the receiver is not know.
            // Get it from the saved list when connectMe handlers gets called
            // by each user.
            onLine[data.to].emit('newPrivateMessage',{from:name, msg:data.msg, type:'Private Msg'})
        }
    });
});

2 Answers 2

2

You can use the Base64 version of your image and send it like this:

onLine[data.to].emit('newPrivateMessage',{from:name, img:data.img.toString('base64'), type:'Private Msg'})

.. and then on the client side receive it and create an image

socket.on("newPrivateMessage", function(data) {
    if (data.img) {
        var img = new Image();
        img.src = 'data:image/jpeg;base64,' + data.img;

        // Do whatever you want with your image.
    }
});

UPDATE

The following is a snippet taken from the link I've commented below. As you can see it takes the image from the input, reads it and sends to the server. After that you can send the same data from the server to another client.

For the full example, please read the article.

JavaScript (client)

...

$('#imageFile').on('change', function(e) {
   var file = e.originalEvent.target.files[0],
     reader = new FileReader();

   reader.onload = function(evt) {
     var jsonObject = {
         'imageData': evt.target.result
       }

     // send a custom socket message to server
     socket.emit('user image', jsonObject);
   };

   reader.readAsDataURL(file);
 });

...

HTML

...

Image file: <input type="file" id="imageFile" /><br/>

...

UPDATE 2

Here is one example I have found:

Java (client)

File file = new File("path/to/the/image");

try {
    FileInputStream imageInFile = new FileInputStream(file);
    byte imageData[] = new byte[(int) file.length()];
    imageInFile.read(imageData);

    // Converting Image byte array into Base64 String
    String imageDataString = Base64.encodeBase64URLSafeString(imageData);
} catch (...) {
    ...
}

The above snippet shows how to read the file and encode the data into a base64 string. So then you can send it just like a string (I assume).

Here is the complete example: How to convert Image to String and String to Image in Java?

Also I have found encodeToString function of Base64.Encoder (java.util package), which you can use.

Sign up to request clarification or add additional context in comments.

9 Comments

Actually how to send from one user to another user. The example given by you is to send image from node.js to user and i want to send from user to another user using node.js
Can you please just tell me how to send image to node.js server?
Please take a look at the code here: ckazbah.com/2014/03/26/…
Can you please just tell me how to send the image to node.js server from json.
What do you mean by from json?
|
0

The easiest way I can think of is to simply Base64 encode the image and send it through the text pipe. You would need to distinguish text and image messages with header information (Maybe send a JSON object?).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.