Code Monkey home page Code Monkey logo

wj.asyncmeteor's Introduction

Introduction

AsyncMeteor is a Meteor/DDP client for .NET that uses the modern Task-based asynchronous programming model. This library is under the MIT license.

Usage

Connecting

var meteor = new Meteor(new Uri("wss://mywebsite.com/websocket"));
await meteor.Connect(); // Or meteor.Connect(cancellationToken)

Calling methods

var eor = await meteor.Call("addTask", "bob", "Buy groceries.", new string[] { "Apples", "Bananas" });
if (eor.Error == null)
{
    Console.WriteLine("Success: " + eor.Result.ToString());
}
else
{
    Console.WriteLine("Error: " + eor.Error.ToString());
}

Subscribing and unsubscribing to publications

var tasksCollection = meteor.RegisterCollection("tasks");
var eor = await meteor.Subscribe("tasks", "bob");
if (eor.Error != null)
    throw new Exception(eor.Error.ToString());
var subscription = eor.Subscription;
// At this point, the subscription is ready.

foreach (var pair in tasksCollection.Data)
{
    var id = pair.Key;
    var task = pair.Value;
    Console.WriteLine("Task " + id + ": " + task.name);
    foreach (var item in task.items)
        Console.WriteLine("  Item: " + item);
}

subscription.Unsubscribe();
meteor.UnregisterCollection("tasks");

Observing changes in a collection

tasksCollection.Added += tasksCollection_Added;
tasksCollection.Changed += tasksCollection_Changed;
tasksCollection.Removed += tasksCollection_Removed;

// ...

private void tasksCollection_Added(string id, IDictionary<string, object> fields)
{
    Console.WriteLine("Task " + id + " has been added:");
    foreach (var field in fields)
        Console.WriteLine(field.Key + " = " + (field.Value == null ? "null" : field.Value.ToString()));
}

private void tasksCollection_Changed(string id, dynamic oldObject, IDictionary<string, object> fields, string[] cleared)
{
    Console.WriteLine("Task " + id + " has been changed:");
    foreach (var field in fields)
        Console.WriteLine(field.Key + " = " + (field.Value == null ? "null" : field.Value.ToString()));
}

private void tasksCollection_Removed(string id, dynamic oldObject)
{
    Console.WriteLine("Task " + id + " has been removed");
}

Known issues

  • Web socket reconnection is not handled yet
  • Nested objects in fields are not handled properly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.