Channels using sockjs (node.js, socket.io)

It occurred to me that it socket.iohas many problems (memory leaks, CPU usage, etc.), so I am converting my application into sockjs(or something else ... if it is difficult to implement).

In socket.ioI was able to easily transmit messages on multiple "channels" using the same connection

socket.emit('news', { hello: 'world' });
socket.emit('other', { hello: 'world' });

How can I achieve this using sockjs? I understand that this functionality is not implemented in sockjs, but is there a reliable structure that does this? I came across this websocket-multiplex but was not satisfied with the reviews I read on several blogs.

thank

+4
source share
2 answers

I really like the direction the team is working on primus , because there is currently a lot of rejection in socket implementations. They allow you to freely switch between several of the most popular websocket libraries (including sockJS) without additional code.

You can use primus with the rooms community plugin to accomplish what you are trying to do.

The plugin will allow you to do something like this:

spark.room('news').write( { hello : 'world' } );

A spark works much like a socket. Whenever a primus receives a connection, it gives you a spark object to manipulate. So here is a complete example:

var Primus = require('primus')
  , http = require('http');

var server = http.createServer(/* request handler */)
  , primus = new Primus(server, { transformer: 'sockjs' });

primus.on('connection', function (spark) {
  spark.room('news').write( { hello : 'world' } );
});

, sockJS primus. , .

ws ​​ primus, . RFC 6455 , .

0 , :

, primus , API- primus. http-, html:

// use this to load the client-side framework in development only
<script src="/primus/primus.js"></script>

, , , primus .

, / , , . .

+6

, 0 , . , , , , . - , , - . , redis sockjs , api RPC - redis.

- , . socket.io , , , . , sockjs.

https://github.com/azuqua/node-token-sockjs

https://github.com/azuqua/jquery-token-sockjs

+3

Source: https://habr.com/ru/post/1540391/


All Articles