Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending a javascript object through websockets with faye

Hi all I'm trying to send a javascript object through websockets:

the faye-websockets documentation says:

send(message) accepts either a String or a Buffer and sends a text or binary message over the connection to the other peer.

server side I'm using node and faye.

var WebSocket = require('faye-websocket'); var http = require('http');  var server = http.createServer(); server.addListener('upgrade', function(request, socket, head) {     var ws = new WebSocket(request, socket, head);     ws.send({topic:'handshake', data:'sdf487rgiuh7'}); }); server.listen(8000); 

client side:

<script>     var ws = new WebSocket('ws://localhost:8000');     ws.onmessage = function(e) {         console.log(e.data); //prints [Object object] string and not the object     }; </script> 

what is my error? Thanks

like image 364
frx08 Avatar asked Oct 23 '12 10:10

frx08


People also ask

Can we send file through WebSocket?

You can send raw binary data through the WebSocket. It's quite easy to manage. One option is to prepend a "magic byte" (an identifier that marks the message as non-JSON).

How do I send a WebSocket message?

To send a message through the WebSocket connection you call the send() method on your WebSocket instance; passing in the data you want to transfer. socket. send(data); You can send both text and binary data through a WebSocket.

Can we send JSON data in WebSocket connection?

To create a WebSocket and open the connection to FME Server use getWebSocketConnection method. The result is an open WebSocket you can use in your application to communicate with FME Server. This example sends a basic JSON object as a string to the server, and displays the response from the server.

How do you use WebSockets in JavaScript?

To open a websocket connection, we need to create new WebSocket using the special protocol ws in the url: let socket = new WebSocket("ws://javascript.info"); There's also encrypted wss:// protocol. It's like HTTPS for websockets.


2 Answers

WebSockets support sending and receiving: strings, typed arrays (ArrayBuffer) and Blobs. Javascript objects must be serialized to one of the above types before sending.

To send an object as a string you can use the builtin JSON support:

ws.send(JSON.stringify(object)); 

To send an object as a typed array you can use a javascript BSON library such as this one:

ws.send(BSON.serialize(object)); 

When you receive a WebSocket message you will need to deserialize it.

To deserialize a JSON string from a WebSocket message:

ws.onmessage = function (e) {     var object = JSON.parse(e.data);     ... }; 

If you are using binary messages over WebSocket, then first you should set the binaryType attribute in order to receive all binary messages as typed arrays:

ws.binaryType = "arraybuffer"; 

Then the deserialization will look like this:

ws.onmessage = function (e) {     var object = BSON.deserialize(e.data);     ... }; 

Here is a blog post about using BSON in Javascript;

like image 104
kanaka Avatar answered Sep 20 '22 20:09

kanaka


I'm basically working with Socket.IO, but it looks like you need to stringify your data in the server and parse it in the client like so:

in the server:

ws.send(JSON.stringify({topic:'handshake', data:'sdf487rgiuh7'})); 

in the client:

console.log(JSON.parse(e.data)); 
like image 36
udidu Avatar answered Sep 19 '22 20:09

udidu