javascript.netwebsocketxsockets.net

Understanding XSockets.NET pubsub: producing and consuming messages from JavaScript


Let's say I've the following sample code (JavaScript):

// Client A 
var conn = new XSockets.WebSocket([wsUri]);

conn.on(XSockets.Events.open, function (clientInfo) {
    conn.publish("some:channel", { text: "hello world" });
});

// Client B (subscriber) 
var conn = new XSockets.WebSocket([wsUri]);

conn.on(XSockets.Events.open, function (clientInfo) {
    conn.on("some:channel", function(message) {
        // Subscription receives no message!
    });
});

Client B never receives a message. Note that this is a sample code. You might think that I don't receive the message because Client B got connected after Client A sent the message, but in the actual code I'm publishing messages after both sockets are opened.

The server-side XSocketsController is working because I'm using it for server-sent notifications.

What am I doing wrong? Thank you in advance!


Solution

  • It looks like you have mixed up the pub/sub with the rpc, but I cant tell for sure if you do not post the server side code as well.

    But what version are you using? 3.0.6 or 4.0?

    Once I know the version and have the server side code I will edit this answer and add a working sample.

    EDIT (added sample for 3.0.6):

    Just wrote a very simple chat with pub/sub.

    Controller

    using XSockets.Core.Common.Socket.Event.Interface;
    using XSockets.Core.XSocket;
    using XSockets.Core.XSocket.Helpers;
    
    namespace Demo
    {
        public class SampleController : XSocketController
        {
            /// <summary>
            /// By overriding the onmessage method we get pub/sub
            /// </summary>
            /// <param name="textArgs"></param>
            public override void OnMessage(ITextArgs textArgs)
            {
                //Will publish to all client that subscribes to the value of textArgs.@event            
                this.SendToAll(textArgs);
            }
        }
    }
    

    HTML/JavaScript

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="Scripts/jquery-2.1.1.js"></script>
        <script src="Scripts/XSockets.latest.min.js"></script>
    
        <script>
            var conn;
    
            $(function() {
                conn = new XSockets.WebSocket('ws://127.0.0.1:4502/Sample');
    
                conn.onopen = function(ci) {
                    console.log('open', ci);
                    conn.on('say', function(d) {
                        $('div').prepend($('<p>').text(d.text));
                    });
                }
    
                $('input').on('keydown', function(e) {
                    if (e.keyCode == 13) {
                        conn.publish('say', { text: $(this).val() });
                        $(this).val('');
                    }
                });
            });
        </script>
    </head>
    <body>
        <input type="text" placeholder="type and hit enter to send..."/>    
        <div></div>
    </body>
    </html>
    

    Regards Uffe