Team XSockets.NET

Pub/Sub

How to define subscription methods on the client that the server can publish to

conn.controller('chat').subscribe('chatmessage', function(data) {
    console.log(data);
});

How to subscribe one time

If you only want to get a message once and then unsubscribe automatically you can use one

conn.controller('chat').one('chatmessage', function(data){
    console.log(data);
});

This will make sure that the client unsubscribe to the topic after getting the first message.

How to subscribe n times

If you want to get a message 1 to n times and then unsubscribe automatically you can use many

conn.controller('chat').many('chatmessage', 3 function(data){
    console.log(data);
});

This will unsubscribe the chatmessage topic after getting 3 messages.

How to know when the subscription is ready

When you compare PUB/SUB with RPC an obvious disadvantage with PUB/SUB is that you do not know if the server has bound the subscription when you do a publish. Therefor you can pass in an additional function to get a callback from the server when the subscription is completed.

This works similar for all subscribe methods.

subscribe(topic, fn, cb);
one(topic, fn, cb);
many(topic, n, fn, cb);

where fn is called when a publish occurs and cb is the callback that confirms the subscription.

How to publish to server methods from the client

Client

conn.controller("chat").publish("chatmessage",{Text: "Hello people!"});

Server

//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;

//The server migth publish the message back to all clients subscribing
public async Task ChatMessage(ChatModel chatModel)
{
    await this.PublishToAll(chatModel, "chatmessage");
}

results matching ""

    No results matching ""