0

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!

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206

1 Answers1

2

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

Uffe
  • 2,275
  • 1
  • 13
  • 9
  • I've working with 3.x. BTW, I wanted to know what's required in the server-side code. I thought both client and server-side code could act as producer and subscriber. In the server code I've a XSocketsController. When I added an action in the controller, I could call `publish` in JavaScript, and send the message to the desired subscribers from the server-side code. But I wanted to know if connecting to a controller and publishing a message using an arbitrary channel name was enough. – Matías Fidemraizer Jul 06 '14 at 12:59
  • In 3.0.6 you can use the "Generic" controller and do pub/sub in a few lines of code. I will update my answer with a complete 3.0.6 sample for JavaScript – Uffe Jul 06 '14 at 13:48
  • I see! Now I understand how it works! Thank you for your effort! ;) – Matías Fidemraizer Jul 06 '14 at 14:11