Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish doesn't send messages #45

Closed
losnappas opened this issue Jun 7, 2019 · 5 comments
Closed

Publish doesn't send messages #45

losnappas opened this issue Jun 7, 2019 · 5 comments

Comments

@losnappas
Copy link
Contributor

Hi,

I don't think the publish is working correctly.

const nknWallet = require('nkn-wallet')
const nkn = require('nkn-client')
const wallet = nknWallet.newWallet('pwd')
const client = nkn({
  privateKey: wallet.getPrivateKey(),
  indentifier: 'client'
})
const receivingClientWallet = nknWallet.newWallet('pwd')
receivingClientWallet.subscribe(
	'topic',
	0,
	10,
	'pwd',
	'identifier'
)
client.on('connect' , () => 
setTimeout(() => {
	client.publish(
	'topic',
	0,
	'hello world'
	)
}, 1000)
)

This makes a request where it gets a result like {"id":"1","jsonrpc":"2.0","result":{"identifier.02c988e6a672fd6b8cf42e68682362ee152a8afb5176b9206c558b4cb89daba47d":"","identifier.0352304707e0af0187b435ea5f3ec8f3a0fbac71e3e801dbc34b3cf8d4000d1128":"","identifier.03c7575885c10f141576d817fa5fbca5144e74e3fd85838530ec8a8027d587aa5a":""}}

But then nothing gets sent to them afterwards. My understanding was that it is a shortcut to send the message to all those addresses automatically.

@yilunzhang
Copy link
Member

Yes your understanding is correct, but subscribe takes some time (usually less than one block interval, i.e. 20s) to be effective because it is an on-chain operation (that's why it's in wallet). publish is an off-chain operation, so once subscriber info is recorded on-chain, publish will be instant

@losnappas
Copy link
Contributor Author

I don't see any messages getting sent even when the response result field has entries, though. I.e. if I change to a 30sec timeout in my example there, although it does find the subscriber in the response, it still never sends any messages.

On a related note, could you please set the publish method to return the response object, instead of nothing.

@yilunzhang
Copy link
Member

You are right, recently metadata is added to subscriber info so it's not parsing correctly. It's fixed in #47 now. Here is a working example:

const nknWallet = require('nkn-wallet')
const nknClient = require('nkn-client')

const sendingClient = nknClient()

const receivingWallet = nknWallet.newWallet('pwd');
const receivingClient = nknClient({
  privateKey: receivingWallet.getPrivateKey(),
  identifier: 'client'
})

receivingWallet.subscribe('topic', 0, 10, 'pwd', receivingClient.identifier).then(r => {
  console.log('subscribe success:', r)
})

receivingClient.on('message', (src, payload) => {
  console.log('reveive message:', payload)
})

sendingClient.on('connect', () => {
  setTimeout(() => {
    console.log('publish');
    sendingClient.publish('topic', 0, 'hello world')
  }, 60 * 1000);
})

60s is usually enough, but in some cases it may take longer because the txn may be packed in a later block, plus block interval can be increased from 20s to 60s in some cases.

Publish is a one-to-many message and thus does not support reply, otherwise it may be used to perform DDoS attack. It may throw error though.

@losnappas
Copy link
Contributor Author

losnappas commented Jun 9, 2019

Yeah, it works now, but it also has a bug. The rpcCall function doesn't seem to always throw errors, instead it returns them.

// rpc.js
module.exports = rpcCall;

async function rpcCall(addr, method, params = {}) {
  let response = await fetch(addr, {
    method: 'POST',
    body: JSON.stringify({
      jsonrpc: "2.0",
      method: method,
      params: params,
    }),
  })
  let data = await response.json();
  return data;
}

So sometimes it returns

{
  error: { code: -42002, message: "INVALID PARAMS" },
  id: "1",
  jsonrpc: "2.0",
}

And then I get TypeError: "can't convert undefined to object" because it calls Object.keys on res.result which is obviously undefined.

You can test it like this

sendingClient.on('connect', () => {
  setTimeout(() => {
    console.log('publish');
    sendingClient.publish(null, 0, 'hello world') // null topic is illegal
  }, 60 * 1000);
})

But this wasn't a bug in the code, but a mistake on my part, huh. so it works correctly now, I guess.

@yilunzhang
Copy link
Member

Fixed in #49 (in ed25519 branch instead of master though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants