Lost future on new binance/spot v23.0.1 newUserDataStream for generate a listenKey
Hi.
We just upgrade @binance/spot v20.0.1 to binance/spot v23.0.1.
client.restAPI.newUserDataStream() does not working anymore.
So we can not generate listenkey for connecting to user data stream.
Where is the guide for new way ?
Errors:
Property 'newUserDataStream' does not exist on type 'RestAPI'.
Property 'putUserDataStream' does not exist on type 'RestAPI'.
Property 'deleteUserDataStream' does not exist on type 'RestAPI'.
Our Code:
const client = new Spot({
configurationRestAPI: {
apiKey: this.apiKey,
apiSecret: this.apiSecret,
basePath: this.baseURL
}
});
const response = await client.restAPI.startUserDataStream();
const data = await response.data();
const listenKey = data.listenKey || '';
console.log(`Created listen key: ${listenKey}`);
const wsUrl = this.isTestnet
? `wss://stream.testnet.binance.vision:9443/ws/${listenKey}`
: `wss://stream.binance.com:9443/ws/${listenKey}`;
this.userDataWs = new WebSocket(wsUrl);
console.log(`[BinanceClient] Connecting to user data stream: ${wsUrl}`);
this.userDataWs.on('open', () => {
console.log(`[BinanceClient] User data stream connected`);
});
this.userDataWs.on('message', (data: Buffer) => {
try {
const message = JSON.parse(data.toString());
console.log(`Received message: ${message}`);
} catch (error: any) {
console.error(`[BinanceClient] Error parsing user data message: ${error.message}`);
}
});
Hi @kyesil, user data streams using WS streams is deprecated for quite a while now and has been removed on the latest version. Instead of WS streams the way forward is to use WS API. You can have a look on the User Data Stream examples. Remember that you'll need to do a session logon before subscribing to the stream, so have a look also on this example. More details in the documentation.
Thank you. Before we have re-conneciton/re-subscribe logic for keepalive. Is this keepalive while program running?
This is working for now:
this.client = new Spot({
configurationRestAPI: {
apiKey: this.apiKey,
apiSecret: this.apiSecret,
basePath: this.isTestnet ? SPOT_REST_API_TESTNET_URL : SPOT_REST_API_PROD_URL,
},
configurationWebsocketAPI: {
apiKey: this.apiKey,
apiSecret: this.apiSecret,
wsURL: this.isTestnet ? SPOT_WS_API_TESTNET_URL : SPOT_WS_API_PROD_URL
}
});
const connection = await this.client.websocketAPI.connect();
console.log(`[BinanceClient:${this.sessionId}] User data stream connected`);
const { stream } = await connection.userDataStreamSubscribeSignature();
console.log(`[BinanceClient:${this.sessionId}] User data stream connected`);
this.isReconnectingUserData = false;
this.userDataWs = stream;
this.userDataWs.on('message', (data: Buffer) => {
try {
this.handleUserDataMessage(data);
} catch (error: any) {
console.error(`[BinanceClient:${this.sessionId}] Error parsing user data message: ${error.message}`);
}
});
connection.on('ping', (event: any) => {
console.log('User data stream ping', event);
});
connection.on('error', (error: any) => {
console.error(`User data stream error`, error);
if (!this.isReconnectingUserData) {
this.reconnectUserDataStream();
}
});
connection.on('close', (e:any) => {
console.log(`User data stream closed`, e);
if (!this.isReconnectingUserData) {
this.reconnectUserDataStream();
}
});
Also
const response = await connection.sessionLogon();
const { stream } = await connection.userDataStreamSubscribe();
Logon return { code: -2028, msg: 'HMAC-SHA-256 API key is not supported.' }
I think testnet cannot generate ed25519 key.
userDataStreamSubscribeSignature() working with old keys.
@kyesil Ed25519 is not currently supported on testnet, so you have only HMAC. As you said userDataStreamSubscribeSignature is going to work on both testnet and prod environments, so feel free to use this one for your use case. As for the reconnection you handle the re-subscription properly.
Hi @kyesil, closing this issue as this is now resolved. Feel free to create a new one in case you see anything wrong!