what's the purpose of `producer.disconnect or `consumer.disconnect` in kafkajs? - kafkajs

what's the purpose of the disconnect functions in kafkajs?
const producer = kafka.producer()
await producer.connect()
await producer.send({
topic: 'test-topic',
messages: [
{ value: 'Hello KafkaJS user!' },
],
})
await producer.disconnect()
what happens if I don't call it at the end of my program?
is it only a flush / memory leak thing or does it impact the kafka side as well?
couldn't find any description of it in the kafkajs docs.

Related

Firebase cloud functions / One works, other (same function) not works

I am very nervous.. I can't test normally firebase cloud functions, because a lot of things don't work. I tested it, I copied same function with a different name, the new function don't work.
Why????
Why working helloworld and why not working tryhello???
cloud functions nodejs index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.tryHello = functions.https.onCall((data, context) => {
let dataexample = {
name: 'examplename',
state: 'examplestate',
country: 'examplecountry'
};
let setDoc = db.collection('newexample').doc(data.text).set(dataexample);
return { text : "success. uid:" + context.auth.uid }
});
exports.helloWorld = functions.https.onCall((data, context) => {
let dataexample = {
name: 'examplename',
state: 'examplestate',
country: 'examplecountry'
};
let setDoc = db.collection('newexample').doc(data.text).set(dataexample);
return { text : "success. uid:" + context.auth.uid }
});
Unity C#:
public void testbutton()
{
var data = new Dictionary<string, object>();
data["text"] = "example";
//I tested "tryHello" and helloWorld"
FirebaseFunctions.DefaultInstance.GetHttpsCallable("tryHello")
.CallAsync(data).ContinueWith((task) =>
{
if (task.IsFaulted)
{
// Handle the error...
print("error");
}
else if (task.IsCompleted)
{
IDictionary snapshot = (IDictionary)task.Result.Data;
print("Result: " + snapshot["text"]);
}
}
Result:
1. First, I write unity: GetHttpsCallable("helloWorld") and save.
I start game, login, then I click testbutton.
result: firebase console: success create example collection, example document, country:examplecountry, name:examplename, state:examplestate. Ok good.
unity log:
1. User signed in successfully: Jane Q. User (qQC3wEOU95eDFw8UuBjb0O1o20G2)
UnityEngine.Debug:LogFormat(String, Object[])
loginfire:b__10_0(Task1) (at Assets/loginfire.cs:155)
2. Result: success. uid:qQC3wEOU95eDFw8UuBjb0O1o20G2
UnityEngine.MonoBehaviour:print(Object)
<>c:<testbutton>b__16_0(Task1) (at Assets/loginfire.cs:411)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()
cloud functions "helloWorld" log:
Function execution started
Function execution took 3020 ms, finished with status code: 200
Ok. I delete "example" collection in firebase console.
2. Second, I write unity: GetHttpsCallable("tryHello") and save.
I start game, login, then I click testbutton.
result: not create collection.
unity log:
*1. User signed in successfully: Jane Q. User (qQC3wEOU95eDFw8UuBjb0O1o20G2)
UnityEngine.Debug:LogFormat(String, Object[])
loginfire:b__10_0(Task`1) (at Assets/loginfire.cs:155)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()
error
UnityEngine.MonoBehaviour:print(Object)
<>c:b__16_0(Task`1) (at Assets/loginfire.cs:396)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()*
cloud functions "tryHello" log:
nothing...
Why?
I don't understand. same, only the name is different!
And.. in many cases it show success but still does not update the database. or just much later. why? Lately, "helloWorld" also often writes an error, if I don't press the testbutton immediately after logging in, it can't read the uid.
I start to get tired of the system right from the start.
Thanks..
Solved!
I needed it configured to cloud console "tryHello" permission settings. (Not same helloworld settings.)
Lately, "helloWorld" also often writes an error, if I don't press the testbutton immediately after logging in, it can't read the uid.
-> I needed declared Firebasauth within testbutton().
Sorry for the question, thanks.

FeathersJS Inserts 2 records into MySql from 1 REST call

When I use the .create(item) method to do an INSERT from the client (within the browser) I see 1 call via websocket or REST go to feathersjs. I see one request go into Feathersjs. For an unknown reason I see 2 rows created in MySql and 2 lines in the log that say: {"message":"after: name_of_service - Method: create","level":"info"}
Using sequelize 4.42.0 and feathers-sequelize 6.0.1
I do not have the issue when running create() from within the server code, only from client.
I found https://github.com/feathersjs-ecosystem/feathers-rethinkdb/issues/80 that looked simular but is for a different DB and the explanation did not fit with MySql.
I switched to MariaDB for other reasons but obviously nothing changed.
I was using FeathersJS v3.x and upgrading to v4.x to see if that would fix it. Nope. As I work around I have been making my own insert methods but it would be nice to use the built in ones.
I tried switching between REST and websocket.
My Hooks:
const { authenticate } = require('#feathersjs/authentication').hooks;
module.exports = {
before: {
all: [ ], // authenticate('jwt') normally use, but deactivated to debug
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
Service:
const createService = require('feathers-sequelize');
const createModel = require('../../models/name_of_service.model');
const hooks = require('./name_of_service.hooks');
module.exports = function (app) {
const Model = createModel(app);
const paginate = app.get('paginate');
const options = {
Model,
paginate: {
default: 100,
max: 2000
}
};
app.use('/name_of_service', createService(options));
const service = app.service('name_of_service');
service.hooks(hooks);
};
I expected it to insert 1 row in MySql table. But got 2. I expected one row in the log for the after hook, but see 2. This has been happening for a couple of months and was thinking, hey, maybe I am not the only one.
Thank you everyone for your help. I found the issue.
I fixed my rooky mistake by changing:
app.configure(socketio());
app.configure(socketio(function(io) {io.sockets.setMaxListeners(555);}));
To:
app.configure(socketio(function(io) {io.sockets.setMaxListeners(555);}));

Quasar + Feathers-Vuex: how to integrate?

I want to integrate Quasar with FeathersJS using Feathers-Vuex
Feathers-Vuex uses a pattern to:
promise to authenticate from localStorage/cookies
.then( /*start the new Vue() app */ )
I created my app with Quasar CLI 1.0.beta16-ish and looked through /src and couldn't find the main entry point for Quasar. I feel like I'm missing something.
What includes src/store/index.js?
quasar.conf.js includes this comment - where is the main.js
// app boot file (/src/boot)
// --> boot files are part of "main.js"
boot: ["axios"],
Feathers-Vuex includes a Nuxt integration guide that may solve the same problem. These packages are all new to me, and I'm excited to learn them!
Thank you!
The part of main.js is included in quasar app.js that you can find in .quasar folder. The src/store/index.js contains the Vuex Store definition. A "store" is basically a container that holds your application state.
For more detail visit - https://quasar-framework.org/guide/app-vuex-store.html https://quasar-framework.org/guide/app-plugins.html
I ended up with two things:
Adding Feathers-Vuex to my backend.
Adding this "boot file" in my Quasar project
The comments are a bread-crumb trail if I ever have to figure it out again :-)
/*
Context:
For 3rd-party API's, we us /src/boot/axios.js
For our own API's, we use FeathersClient (socket.io & REST)
https://docs.feathersjs.com/guides/basics/clients.html
https://docs.feathersjs.com/api/authentication/client.html#appconfigureauthoptions
Our FeathersClient is in `/src/lib/feathersClient.js`
and imported into `/src/store/index.js`
which is imported by Quasar's build system. /src/quasar.conf.js setting(?)
Feathers-vuex integrates Vuex with FeathersClient:
https://feathers-vuex.feathers-plus.com/auth-module.html
Feathers-Vuex proxies it's authentication/logout actions to FeathersClient
https://github.com/feathers-plus/feathers-vuex/blob/master/src/auth-module/actions.js
The parameters for these actions are here:
https://docs.feathersjs.com/api/authentication/client.html#appauthenticateoptions
In addition to this module, you can use FeathersVuex state in UI from here:
https://feathers-vuex.feathers-plus.com/auth-module.html
This module:
Create a Feathers Auth integration for Vue as a Quasar Boot Module.
// Use case: test if user is authenticated
if (Vue.$auth.currentUser()) { ... }
// Use case: get current user's email
name = Vue.$auth.currentUser("email") || "anonymous"
// Use case: Login
Vue.$auth.login({
strategy: 'local',
email: 'my#email.com',
password: 'my-password'
});
// Use case: Logout
// logs out and sends message
let p = Vue.$auth.logout();
// After logout, go home
p.then(() => {
// User data still in browser
router.push({ name: "home"});
// To clear user data, do a hard refresh/redirect - https://feathers-vuex.feathers-plus.com/common-patterns.html#clearing-data-upon-user-logout
location && location.reload(true)
});
*/
export default ({ app, router, store, Vue }) => {
// Create the API demonstrated above
const auth = {
currentUser(prop) {
let u = store.state.auth.user || false;
if (u && prop) return u[prop];
return u;
},
login(authData, quiet) {
return store
.dispatch("auth/authenticate", authData)
.then(() => {
Vue.prototype.$q.notify({
message: "Welcome back!",
type: "info"
});
})
.catch(err => {
if (!quiet) {
console.log(err);
Vue.prototype.$q.notify({
message: "There was a problem logging you in.",
type: "error"
});
}
});
},
logout(quiet) {
return store.dispatch("auth/logout").then(() => {
if (!quiet)
Vue.prototype.$q.notify({
message: "You've been logged out.",
type: "info"
});
});
},
register(authData) {}
};
// Auth from JWT stored in browser before loading the app. true => suppress token not found error
auth.login("jwt", true);
// Add API to Vue
Vue.prototype.$auth = auth;
// If you would like to play with it in the console, uncomment this line:
// console.log(auth);
// Then, in the console:
/*
temp1.login({
strategy: "local",
email: "feathers#example.com",
password: "secret"
})
*/
// If you haven't created this user, see here:
// https://docs.feathersjs.com/guides/chat/authentication.html
// For this REST api endpoint
/*
curl 'http://localhost:3001/users/' -H 'Content-Type: application/json' --data-binary '{ "email": "feathers#example.com", "password": "secret" }'
*/
};

web3.eth.accounts.create method doesn't actually create new account

I try to create an eth account via RPC in private network.
What I have done so far are:
launch geth node and create private network.
create simple javascript program using web3 1.0.0, typescript
run and get result as below but the account isn't created
Code:
const result = await web3.eth.personal.unlockAccount(senderId, senderPassword, duration)
if (result === true) {
// const newAccountResult = await web3.eth.personal.newAccount('password')
const newAccountResult = await web3.eth.accounts.create('user01')
console.log(newAccountResult)
}
Result:
web3.eth.accounts.create returns the following result
{ address: '0xf10105f862C1cB10550F4EeB38697308c7A290Fc',
privateKey: '0x5cba6b397fc8a96d006988388553ec17a000f7da9783d906979a2e1c482e7fcb',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt] }
But web3.eth.getAccounts method returns only 1 account.
[ '0xaf0034c41928Db81E570061c58c249f61CFF57f2' ]
Seems web3.eth.accounts.create method has succeeded as the result includes account address and private key.
But I don't understand why web3.eth.getAccounts method doesn't include the created account.
I also checked geth via console, the result is same.
> eth.accounts
["0xaf0034c41928db81e570061c58c249f61cff57f2"]
And eth.personal.newAccount didn't work.
Do I need to do something after web3.eth.accounts.create?
I appreciate any help.
If i got it right, web.eth.accounts.create is a way to create accounts without storing them on the local node, so its basically a way to get a valid keypair on-the-fly without storing anything on the keystore)
web3.eth.personal.newAccount() should be availabel if you have the personal-API activated on your geth node (which is default behavior for ganache, with geth you need to activate it via geth --dev/testnet --rpc --rpcapi eth,web3,personal (note: of course you should be very careful with allowing personal-API on mainnet, make sure that RPC access is restricted so only you/privileged users can access it)
(async () => {
let newAccount = await web3.eth.personal.newAccount();
console.log(newAccount);
let accounts = await web3.eth.getAccounts();
console.log(accounts);
})();
Should give something like
0xb71DCf0191E2B90efCD2638781DE40797895De66
[
...
'0xb71DCf0191E2B90efCD2638781DE40797895De66' ]
Refs https://medium.com/#andthentherewere0/should-i-use-web3-eth-accounts-or-web3-eth-personal-for-account-creation-15eded74d0eb

Sending .send requests in loop only sets the last value of the loop i amount of times

let batch = new this.web3.BatchRequest();
const arr = [
{name: "test1", att: 100, def: 100},
{name: "test2", att: 100, def: 100},
{name: "test3", att: 100, def: 100},
{name: "test4", att: 100, def: 100},
]
arr.forEach((d) => {
batch.add(this.contract.methods.createCountry(d.name, d.att, d.def, 10, this.account).send.request(this.contractObject, (err, res) => {
if (err) {
throw err;
} else {
console.log(res);
}
}));
});
console.log(batch);
batch.execute();
I know it's not the smart contract that is the problem since i tested it thoroughly in Remix and with seperate country pushes. I use web3 and Metamask.
All values in the contract are set to "test4"
When sending many transactions back-to-back, you have to set the nonce for each transaction, and also increment it. Normally the nonce is set for you by the node, but it doesn't work for multiple sequential transactions.
The reason why only the last transaction is actually sent, is because the nonce can be used as a way to override transactions before they are mined (like if you sent it with too little gas).
I have answered this question earlier, with code example
Repeating transactions hangs - web3js, local geth
I found a hacky solution for this problem and created an issue on the web3 github.
See my the issue and my solution here: https://github.com/ethereum/web3.js/issues/1636
I couldn't find a neat solution but I'm sure once the issue gets picked up a solution will be presented.