Search code examples
extjswebsocketextjs4chat

ExtJS Websocket Chat Application


Im setting an MVC application with Extjs 4.2 that has a chat support for my client. FOr this I'm using ExtJS Websocket and I followed the tutorial from this https://market.sencha.com/extensions/ext-ux-websocket but I could seem to work to broadcast a message to all connected client on different browsers. Here are my codes:

        this.ws = Ext.create('Ext.ux.WebSocket', {
            url : 'ws://localhost:6966',
            autoReconnect : true,
            autoReconnectInterval : 1000,
            keepUnsentMessages : true,
            listeners : {
                open : function(ws) {
                    ws.send('notify', {
                        'message' : 'Connected to server!',
                        'id' : '123456'
                    });
                },
                message : function(ws, data) {
                    var data = Ext.decode(data);
                    console.log(data)
                },
                close : function(ws) {
                    var panel = Ext.getCmp('panel' + ws.url);

                    if ((panel != null) || (panel != undefined)) {
                        panel.destroy();
                    }
                }
            }
        });

        this.ws.on('notify', function(data) {
            Ext.get('chat-history').dom.innerHTML += '> ' + data.msg + '<br/>';
        });

        Ext.ux.WebSocketManager.register(this.ws);

And I use this to send/broadcast messages:

var data = {
            id : '123456',
            msg : 'sample message'
        };

        Ext.ux.WebSocketManager.broadcast('notify', data);

Solution

  • You could try out this this link.

    You need NodeJS and NPM.

    Then, install every dependencies:

    $ npm install
    

    Open a websocket for each port you want:

    $ node demo/server.js 9001 9002 9003
    

    Then connect your client using your code you posted.