Search code examples
migrationsocket.iosockjscustom-events

Supporting events on sock.js


I'm thinking of migrating from Socket.io to Sock.js.

But my code is heavily based on custom events, and I'm wondering how to add them on Sock.js, since it doesn’t' support this feature out of the box.

What would you find as the less time-consuming way to do this?


Solution

  • I resolved by using chuckt.

    npm install chuckt
    

    In the source code of the server:

    var sockjs = require('sockjs');
    var chuckt = require('chuckt');
    
    var sock = sockjs.createServer();
    sock.on('connection', function(conn) {
        var chuckt = new ChuckT(conn);
        // ... do chuckt stuff like add listeners or emit events
    

    In the client:

    <script src="chuckt.js"></script>
    <script>
        var sock = new SockJS('http://example.com/socket');
        var chuckt = epixa.chucktify(sock);
    </script>
    

    Be careful that once is not available at the moment in the client.