Giter Site home page Giter Site logo

Cote Just Doesn't Work about cote HOT 22 CLOSED

dashersw avatar dashersw commented on August 25, 2024
Cote Just Doesn't Work

from cote.

Comments (22)

iwanjunaid avatar iwanjunaid commented on August 25, 2024 2

I try explicitly set the broadcast IP address and now it works! @dashersw thanks a million

from cote.

iwanjunaid avatar iwanjunaid commented on August 25, 2024 1

sorry for the missing details. Here is the code:

time-service.js

const cote = require('cote');
const timeService = new cote.Responder({name: 'Time Service'});

timeService.on('time', (req, cb) => {
  cb(new Date());
});

client.js

const cote = require('cote');
const client = new cote.Requester({name: 'Client'});

client.send({type: 'time'}, (time) => {
  console.log(time);
});

from cote.

iwanjunaid avatar iwanjunaid commented on August 25, 2024 1

Here is what i got after run node time-service.js

Hello! I'm Time Service#b53a3d5d-91d8-417a-9d12-189ccdf70a97 on 8000
========================

and here is the ouput for client.js

Hello! I'm Client#1dbb4524-39bc-4696-b0b2-588826db86dc
========================

from cote.

dashersw avatar dashersw commented on August 25, 2024 1

you are right, actually you found the issue yourself. you can't return simple values in cote, they have to be promises.

responder.on("convert", req => req.amount * rates[`${req.from}_${req.to}`]);

will not work, it needs to be

responder.on("convert", req => Promise.resolve(req.amount * rates[`${req.from}_${req.to}`]));

from cote.

matejthetree avatar matejthetree commented on August 25, 2024 1

from cote.

dashersw avatar dashersw commented on August 25, 2024 1

Note: By default, every Requester will connect to every Responder it discovers, regardless of the request type. This means, every Responder should respond to the exact same set of requests, because Requesters will load-balance requests between all connected Responders regardless of their capabilities, i.e, whether or not they can handle a given request.

If you have multiple Responders with varying response handlers, you will experience lost requests. In cote, this separation between responsibilities is called segmentation, or partitioning. If you wish to segment your requests in groups, you can use keys. Check out keys for a detailed guide on how and when to use segmentation.

from cote.

robophil avatar robophil commented on August 25, 2024

@iwanjunaid "just doesn't work" is a bit too broad don't you think?
Can you share your code so we can see what you're doing?
see https://github.com/dashersw/cote-workshop for a sample work. Hope this helps

from cote.

robophil avatar robophil commented on August 25, 2024

hi @iwanjunaid ,
The sample code above works. Just make sure you run the time-service.js first before running the client.js so by that time, the timeservice would be available
or better yet, replace your client.js code with

const cote = require('cote');
const client = new cote.Requester({name: 'Client'});

setInterval(()=>{
   client.send({type: 'time'}, (time) => {
  console.log(time);
})
}, 1000)

from cote.

dashersw avatar dashersw commented on August 25, 2024

In fact, you don't even need to care about the order of execution. The Requesters queue queries until a Responder is active — so you can run the client first and then the service, it will work.

@iwanjunaid this code works. could you tell us a bit more about your environment?

from cote.

iwanjunaid avatar iwanjunaid commented on August 25, 2024

@dashersw Ubuntu 16.04, node v6.10.1 and cote 0.15.1.

from cote.

dashersw avatar dashersw commented on August 25, 2024

Are you running in the cloud, on your local machine or a virtual machine? Are you using Docker, multiple machines, etc?

from cote.

iwanjunaid avatar iwanjunaid commented on August 25, 2024

@dashersw no, it is just a local machine on my laptop.

from cote.

iwanjunaid avatar iwanjunaid commented on August 25, 2024

Is there a way to run cote verbosely ?

from cote.

dashersw avatar dashersw commented on August 25, 2024

Looks like IP broadcast doesn’t work on your machine. You could try setting it up, or you could fall back to multicast, or even redis — see the readme for that.

For debugging, there’s an environment variable DEBUG that you can use, setting it to * will give everything.

from cote.

arefaslani avatar arefaslani commented on August 25, 2024

@dashersw If IP broadcast doesn't work on my machine (with macOS Mojave), how to make it work?

from cote.

dashersw avatar dashersw commented on August 25, 2024

@arefaslani you can try the redis fallback.

from cote.

matejthetree avatar matejthetree commented on August 25, 2024

from cote.

arefaslani avatar arefaslani commented on August 25, 2024

@matejthetree This is the responder:

// server.js
const cote = require('cote');

const responder = new cote.Responder({ name: 'currency conversion responder' });

const rates = { usd_eur: 0.91, eur_usd: 1.10 };

responder.on("convert", req => req.amount * rates[`${req.from}_${req.to}`]);

and this is the requester:

// client.js
const cote = require('cote');

const requester = new cote.Requester({ name: 'currency conversion requester' });

const request = { type: 'convert', from: 'usd', to: 'eur' };

setInterval(
  async () => {
    const amount = Math.ceil(Math.random() * 100);
    const res = await requester.send({ ...request, amount });
    console.log(`result: ${res}`);
  }, 2000
)

When I run both client and server, services can't find each other by default:
screen shot 1397-09-13 at 07 36 47

But when I use Redis fallback they will find each other but nothing happens:
screen shot 1397-09-13 at 09 15 20

After using Redis fallback once, although without Redis, they will find each other sometimes but still nothing happens:
screen shot 1397-09-13 at 09 17 13

And in the end, when I return a Promise in the responder, everything works and I don't know why!

const cote = require('cote');

const responder = new cote.Responder({ name: 'currency conversion responder' });

const rates = { usd_eur: 0.91, eur_usd: 1.10 };

responder.on(
  "convert",
  req =>
    new Promise(
      (resolve, reject) => resolve(req.amount * rates[`${req.from}_${req.to}`])
    )
);

screen shot 1397-09-13 at 09 19 24

I'm totally confused!

from cote.

arefaslani avatar arefaslani commented on August 25, 2024

@dashersw and @matejthetree thank you for quick response and sorry for late reply! Yesterday I was all day to what goes wrong. I've created a simple problem that looks like the original one. Suppose we have three services:

  • age responder: gets year of birth as integer and calculates the age
  • greeter: gets the age from age responder and listens to greet event to return a greeting message
  • greeter client: sends a name to the greeter service to show the greeting message
// age responder service
const { Responder } = require("cote");

const ageResponder = new Responder({ name: "age responder" });

ageResponder.on("calculate age", req =>
  Promise.resolve(new Date().getFullYear() - req.yearOfBirth)
);
// greeter service
const { Requester, Responder } = require("cote");

const ageRequester = new Requester({ name: "age requester" });
const responder = new Responder({ name: "greeter" });

(async () => {
  const age = await ageRequester.send({
    type: "calculate age",
    yearOfBirth: 1989
  });

  responder.on("greet", req =>
    Promise.resolve(`Hello ${req.name}! Your age is ${age} years.`)
  );
})();
// greeter client service
const { Requester } = require("cote");

const requester = new Requester({ name: "greet requester" });

(async () => {
  const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
  do {
    const res = await requester.send({ type: "greet", name: "Aref" });
    console.log(res);
    await delay(2000);
  } while (true);
})();

then I run all three services, each with a command like COTE_DISCOVERY_REDIS=true node service-file-name.js and then services find each other but they didn't log anything! What is the problem here? How can I create a service that is at the same time a requester and a responder?

from cote.

dashersw avatar dashersw commented on August 25, 2024

you can read more about this here: https://github.com/dashersw/cote#keys

basically, if you don't have keys for your requesters / responders, all your responders should be able to respond to the same messages. you can't have greet and calculate age messages in separate responders. if that would be the case, you should segregate these services.

from cote.

arefaslani avatar arefaslani commented on August 25, 2024

@dashersw Thank you! I'll notify you about the result.

from cote.

SHAPPY0 avatar SHAPPY0 commented on August 25, 2024

I try explicitly set the broadcast IP address and now it works! @dashersw thanks a million

hey,
can you please share the code?

from cote.

Related Issues (20)

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.