Team XSockets.NET

Pub/Sub

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

conn.Controller("stockticker").Subscribe<Stock>("newStock", stock => Console.WriteLine("Symbol: {0} price: {1}\n", stock.Symbol, stock.Price));

How to subscribe one time

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

conn.Controller("stockticker").One<Stock>("newStock", stock => Console.WriteLine("Symbol: {0} price: {1}\n", stock.Symbol, stock.Price));

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("stockticker").Many<Stock>("change", 3, data => Console.WriteLine(data));

This will unsubscribe the change 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<T>(string topic, Action<T> callback, Action<IMessage> confirmCallback);
One<T>(string topic, Action<T> callback, Action<IMessage> confirmCallback);
Many<T>(string topic, uint limit, Action<T> callback, Action<IMessage> confirmCallback);

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

How to remove a subscription

When you no longer want to subscribe to a topic you just use the unsubscribe method to tell the server to remove the subscription.

conn.Controller("chat").Unsubscribe("chatmessage");

How to publish to server methods from the client

Client

await conn.Controller("chat").Publish("chatmessage",new {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 ""