Amol M Kulkarni
Amol M Kulkarni

Reputation: 21639

How to Push data from C# to ZeroMQ and Pull from Node.JS or vice-versa?

Scenario:

I am trying to send a data (say String type) from C# сonsole application to Node.JS server through ZeroMQ.

Information:

Using clrzmq for c# and ZeroMQ libs for C# and Node.JS respectively

I am able to perform push-pull from Node.JS, also push - pull from C#.

So, one thing is confirmed that ZeroMQ - The Intelligent Transport Layer is installed on the machine (Windows 7 64-bit)

Issue:

I am not able to push data from C# Console app to Node.JS app (even tried vice-versa), both are on the same machine and on the same address i.e tcp://127.0.0.1:2222

Node.js code:

var zmq = require('zeromq.node');
var pull_socket = zmq.socket('pull');    
pull_socket.connect('tcp://127.0.0.1:2222');    
pull_socket.on('message', function (data) {
    console.log('received data:\n');
    console.log(data);
});

C# code:

namespace DataServiceEngine
{
    class Program
    {
        static void Main(string[] args)
        {
            //clsApp App = new clsApp();
            //App.appId = "001";
            //App.name = "Back Office";

            //Console.WriteLine("appId :" + App.appId + "\n");
            //Console.WriteLine("name:" + App.name + "\n");

            try
            {
                // ZMQ Context and client socket
                using (var context = new Context(1))
                {
                    using (Socket client = context.Socket(SocketType.PUSH))
                    {
                        client.Connect("tcp://127.0.0.1:2222");

                        string request = "Hello";
                        for (int requestNum = 0; requestNum < 10; requestNum++)
                        {
                            Console.WriteLine("Sending request {0}...", requestNum);
                            client.Send(request, Encoding.Unicode);

                            string reply = client.Recv(Encoding.Unicode);
                            Console.WriteLine("Received reply {0}: {1}", requestNum, reply);
                        }
                    }
                }
            }
            catch (ZMQ.Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
    }
}

Question: Can anyone tell me what may be the reason or where am I doing wrong?

Upvotes: 2

Views: 1569

Answers (1)

Masiar
Masiar

Reputation: 21352

I had the same issue (but I issued a communication Node.JS -> Node.JS). To solve the problem I used to do sendersocket.connect("tcp://"+host+":"+port); at the sender and receiversocket.bindSync("tcp://*:"+port); at the receiver.

Hope this fix your problem.

Upvotes: 1

Related Questions