How to send JavaScript Object with Socket io from server to client

0 votes

How to send JavaScript Object with Socket.io from server to client? I'm using Socket.io as WebSocket(sending with .send() and listen with message event). When I'm trying to do something like on server-side:

var myObject = {
    message: 'Hello World!'
}

socket.send(myObject);

on client-side I'm getting only this String: [object Object]

Nov 30, 2020 in Node-js by kartik
• 37,510 points
3,378 views

1 answer to this question.

0 votes

Hello @kartik,

You actually need to emit an event instead:

 socket.emit('yourEvent', myObject);

If you use .send(), you are simply sending the string representation of your object, which is where the problem is occurring. Note that you can use .send(), but you would have to JSON-encode the object first, and decode it on reception.

Unless you have a specific reason, it's best to use the standard Socket.IO .emit() method, as it does all of this for you. That's what it is there for.

answered Nov 30, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
0 answers

How to transfer data from a controller to a socket.io in nodejs?

I'm developing a web application that will ...READ MORE

Jun 23, 2022 in Node-js by Vaani
• 7,020 points
2,268 views
0 votes
1 answer

How to disconnect from tcp socket in NodeJs?

Hello, Try this out: net.createConnection() returns a Socket object. client.destroy() is what ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,880 points
3,587 views
0 votes
1 answer
0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
2,441 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 781 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,880 points
2,280 views
0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,880 points
3,414 views
0 votes
1 answer

How to shrink Navigation menu or bar on Scroll?

Hey, You can follow the steps below in ...READ MORE

answered Feb 19, 2020 in PHP by varun
3,453 views
0 votes
1 answer

How to programmatically send a 404 response with Express/Node?

Hello @kartik, The method is: res.sendStatus(404); Express will send a ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,880 points
3,196 views
0 votes
1 answer

How to get access to webpack-dev-server from devices in local network?

Hello @kartik, You can set your ip address ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,880 points
5,571 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP