Socket.io message body

1,379 views
Skip to first unread message

Gonçalo Vieira

unread,
Oct 11, 2011, 7:58:55 AM10/11/11
to Socket.IO
Hi, am new to socket.io development but have already made a couple of
fun little projects (chat, alert system).

I would like to know what's the message format that it uses when it
comunicates,
is it in JSON format?

how can I send an event from a system that can't import libraries?

is it something like this?
{event: "123", data:{var1: "Hello", var2: "World"}} ?

Thanks for your replies in advance!

Jason Chen

unread,
Oct 11, 2011, 8:22:43 AM10/11/11
to sock...@googlegroups.com
>> I would like to know what's the message format that it uses when it comunicates, is it in JSON format?
By default the format is a simple string, for example: socket.emit('your message'); 
You can however, just do socket.json.emit({"key": "value"}); to send messages in JSON format.

>> how can I send an event from a system that can't import libraries?
What do you mean by send an event and what do you mean by can't import libraries? If you want to use socketio's event system (which you don't have to do), the sender would do socket.emit(eventname, data, function callback() { //do something... }); and the receiver would do socket.on(eventname, function fn() { //do something... }); Again if you want data to be in JSON format instead of a string, you can just do socket.json.emit instead.

All this and much more can be found on the readme section of https://github.com/LearnBoost/socket.io

Best,
Jason Chen


2011/10/11 Gonçalo Vieira <sand...@gmail.com>

Gonçalo Vieira

unread,
Oct 11, 2011, 9:43:29 AM10/11/11
to Socket.IO
Well what I meant is this,

I have a 20 year old software that cannot be modified other than some
very basic things, like messages that can be sent through sockets...

if I send a message without any sort of a tag, through a socket, will
socket.io be able to interpret it as a normal "message" event?

what I want is to know how it receives the data, not how it's meant to
interact since I can easily change the sent message (from my software)
to a format that is easy to understand for socket.io.

On 11 Out, 13:22, Jason Chen <jhch...@gmail.com> wrote:
> >> I would like to know what's the message format that it uses when it
>
> comunicates, is it in JSON format?
> By default the format is a simple string, for example: socket.emit('your
> message');
> You can however, just do socket.json.emit({"key": "value"}); to send
> messages in JSON format.
>
> >> how can I send an event from a system that can't import libraries?
>
> What do you mean by send an event and what do you mean by can't import
> libraries? If you want to use socketio's event system (which you don't have
> to do), the sender would do socket.emit(eventname, data, function callback()
> { //do something... }); and the receiver would do socket.on(eventname,
> function fn() { //do something... }); Again if you want data to be in JSON
> format instead of a string, you can just do socket.json.emit instead.
>
> All this and much more can be found on the readme section ofhttps://github.com/LearnBoost/socket.io
>
> Best,
> Jason Chen
>
> 2011/10/11 Gonçalo Vieira <sandw...@gmail.com>

Tomas Huang (冠)

unread,
Oct 11, 2011, 9:53:29 AM10/11/11
to sock...@googlegroups.com

you could try nowjs what transfer data with Json, support duplex event call and based on socket.io.

在 2011-10-11 下午8:00,"Gonçalo Vieira" <sand...@gmail.com>写道:

Gonçalo Vieira

unread,
Oct 11, 2011, 12:36:22 PM10/11/11
to Socket.IO
Well we'd like to keep ourselves with socket.io for now since it's
working for one of the applications we're having right now.
I just tested the sending of a clean String through a socket that was
created, no go, it does appear that the message needs a structure to
be received by node.js

Please do note, we're attempting to connect through a socket, we're
not using socket.io in the "client" side, since it's a closed platform
and we can't really add any extra stuff...

Will we need to fetch the data that we're trying to send through a
socket to a webpage and then send from that using the socket.io-
client? We'd like to keep connections to a minimum tho...

On 11 Out, 14:53, Tomas Huang (冠) <huangguan3112...@gmail.com> wrote:
> you could try nowjs what transfer data with Json, support duplex event call
> and based on socket.io.
> 在 2011-10-11 下午8:00,"Gonçalo Vieira" <sandw...@gmail.com>写道:

Roey Berman

unread,
Oct 11, 2011, 12:49:26 PM10/11/11
to sock...@googlegroups.com

Are u connecting a raw tcp socket or using http? Because you have to connect using http.
For persistent connections you might even require a websocket client

Erich Buri

unread,
Oct 12, 2011, 5:21:08 AM10/12/11
to sock...@googlegroups.com
Hi,
if you don't want to introduce new libraries in your client application but have the ability to use "normal" tcp sockets there, you basically could do the opposite of what is described here:
http://stackoverflow.com/questions/7662088/expose-socket-via-web-sockets

One of the answers mentions the project websockify which does exactly the opposite of what you want to do. That means you could write a nodejs server that listens on a tcp-socket for your client application and uses the node-socketio-client (https://github.com/saschagehlich/node-socket.io-client) to communicate with your socket.io server. You'd also need to implement a message-protocol over the tcp-socket.

you can also implement this bridge in an other language. see this page https://github.com/learnboost/socket.io/wiki/ for informations about implementations in other languages. There is also the weberknecht for Java (http://code.google.com/p/weberknecht/).

Arnout Kazemier

unread,
Oct 12, 2011, 5:30:17 AM10/12/11
to sock...@googlegroups.com
Small note on this, sascha's socket.io-client was written for Socket.IO 0.6 which is not compatible with 0.7+
Also we made our client script compatible with node.js so you can just do npm install socket.io-client and use
the same API as your front end to communicate with the server.
Reply all
Reply to author
Forward
0 new messages