Hubert Bratek
Hubert Bratek

Reputation: 1104

sockJS own event names

I am using sockjs-node and I want to emit on my own events(using conn.write(...) i am able only to emit on 'data') is it possible to do so? And how?

echo.on('connection', function(conn) {
conn.emit('message', {msg:'xd'}); //I know that it doesnt work, just i would like to know is it possible to achieve emitting on my own events using sockjs
conn.on('close', function() {
    console.log('close');
});
conn.end();
});

for example, sth similar to the line with conn.emit

Upvotes: 0

Views: 449

Answers (1)

jfriend00
jfriend00

Reputation: 707436

sockJS works like a webSocket where it just delivers data packets to you. If you want a messaging layer on top of that, you could build one fairly easily, but it is not part of sockJS itself.

To build your own message layer, you could just built a simple abstraction on top of the sockJS transport that supports .emit(msg, data) and then sends a message over sockJS that has two parts to it, the message name and the data, perhaps converted to JSON for sending the JSON over sockJS.

On the receiving end, you would do just the opposite and you could have your own eventEmitter so you'd receive the sockJS data, break it apart into message name and data and then emit the data to the message name.

FYI, it might just make sense to use socket.io since it is already a messaging layer on top of webSocket (and a few other features such as auto-reconnect).

Upvotes: 1

Related Questions