Code Monkey home page Code Monkey logo

ddpclient.net's Introduction

DDP Client for .NET

DDPClient.NET is a library to connect to DDP server. DDP is the protocol used in Meteor. Using DDPClient.NET you can subscribe to published items or call a Meteor method and display the same in your ASP.NET or Desktop applications.

How to Subscribe to a published item

It is very easy with the help of DDPClient.NET. See the code below.


	static void Main(string[] args)
	{
		IDataSubscriber subscriber = new Subscriber();
		DDPClient client = new DDPClient(sub);

		client.Connect("localhost:3000");
		client.Subscribe("allproducts");
	}
	
    public class Subscriber:IDataSubscriber
    {
        public void DataReceived(dynamic data)
        {
            try
            {
                if (data.type == "sub")
                {
                    Console.WriteLine(data.prodCode + ": " + data.prodName + ": collection: " + data.collection);
                }
            }
            catch(Exception ex)
            {
                throw;
            }
        }
    }		
	

As you can see in the main function, I specified the url where my Meteor application is running. Then I subscribed to ‘appproducts’, which I Published in my Meteor application as shown below.


	if(Meteor.is_server)
	{
		Meteor.publish("allproducts", function(){
			return Products.find();
		});
	}

I used a console application here to demonstrate how to use DDPClient.NET. If you run the console application it will write all the items in the Products to the console. Also the console will show product in realtime if any new products inserted to the database.

How to call a method

Let’s see how to invoke a method in Meteor.


	static void Main(string[] args)
	{
		IDataSubscriber subscriber = new Subscriber();
		DDPClient client = new DDPClient(sub);

		client.Connect("localhost:3000");
		client.Call("addProduct", "NS5", "IRobot");
	}
	
    public class Subscriber:IDataSubscriber
    {
        public void DataReceived(dynamic data)
        {
            try
            {
                if (data.type == "method")
                    Console.WriteLine(data.result);
            }
            catch(Exception ex)
            {
                throw;
            }
        }
    }		

As you can see I am invoking a method called ‘addProduct’ with two parameters. See the meteor code below, as you can see addProduct accepts two parameters prodCode and prodDesc.


if(Meteor.is_server)
{
    Meteor.publish("allproducts", function(){
        return Products.find();
    });

    Meteor.methods({
        addProduct: function (prodCode, prodDesc) {
            return "Product Name: " + prodDesc + " Product Code: " + prodCode;
        },

        bar: function () {
            return "baz";
        }
    });
}

ddpclient.net's People

Contributors

sonyarouje avatar sgaluza avatar stfnbrgh avatar

Watchers

James Cloos avatar Jignesh avatar

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.