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

Chat messages works well #60

Closed
vladosnik opened this issue Sep 3, 2022 · 9 comments
Closed

Chat messages works well #60

vladosnik opened this issue Sep 3, 2022 · 9 comments

Comments

@vladosnik
Copy link

Hello i am from Ukraine and my english is bad and i faced with problem, when your stream watching few people it's ok to use this library and i can get every message from chat, but if my stream watching from 300 to 1.5k people and every body writing messages to chat, this TikTol-Live-Connector lib starts skipping chat messages, when i compare it with tiktok chat on my device it looks like messages come in batches and this tiktok live connector library just showing the last message, you know tiktok server sending like 5-10 messages same time and library only shows last one, please test this on very active stream and see what i am trying to describe! if you understand me please tell me what to do to fix it somehow, maybe i can get messages in some kind of array?

@zerodytrash
Copy link
Owner

will check this.

@vladosnik
Copy link
Author

thank you for fast reply, i love your live connector! it's the best! I was trying do do this kind of socket connection but i failed, when i saw this library it was like blessing to me

@vladosnik
Copy link
Author

vladosnik commented Sep 3, 2022

i am also sending tiktok messages from backend to frontend via websocket library maybe it has some delay or it's just skips messages? i can show you my code but it's very boilerplate i don't think that problem in my code

I AM USING http-server -a localhost -p 8000 to start frontend index.html file

THIS IS MY BACKEND CODE

const {WebcastPushConnection} = require('tiktok-live-connector')
let tiktokUsername = 'nester4340'


let stream = new WebcastPushConnection(tiktokUsername)

stream.connect().then(state=>{
}).catch(e => {
	console.log(e)
})


var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    response.writeHead(404);
    response.end();
});

server.listen(1234, function() {});

wsServer = new WebSocketServer({
    httpServer: server,
    autoAcceptConnections: false
});

function originIsAllowed(origin) {
  return true;
}

wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      request.reject();
      return;
    }
    
    var connection = request.accept('echo-protocol', request.origin);
    
    stream.on('chat',data=>{
		connection.sendUTF(JSON.stringify({type:'chat',payload:data}));
	})


    stream.on('like', data => {
		connection.sendUTF(JSON.stringify({type:'like',payload:data}));
	})

    connection.on('close', function(reasonCode, description) {
        console.log('disconnect');
    })

})

THIS IS FRONTEND WAY TO RECEIVE MESSAGES


const ws = new WebSocket('ws://localhost:1234','echo-protocol')

ws.addEventListener('open',()=>{
	console.log('ws connected!')
	ws.send('hello from client')
})

ws.addEventListener('close',()=>{
	console.log('соединение прервано!')
})

ws.addEventListener('message', function (event) {

	//console.log(`сообщение из сервера`)
	 try {
        const messageFromServer = JSON.parse(event.data)

        if(messageFromServer.type === 'chat') {
        	const payload = messageFromServer.payload
			m.newMessage(payload.userDetails.profilePictureUrls[1],payload.uniqueId,payload.comment) //this is how i show messages
			
        } else if(messageFromServer.type === 'like') {
        	const payload = messageFromServer.payload
        
        	l.like(payload.likeCount) //this is how i show likes with my app
        }

    } catch(e) {
    	console.log(e,event.data)
    }

})

@zerodytrash
Copy link
Owner

Please use this code for testing:

const { WebcastPushConnection } = require('tiktok-live-connector');

let connection = new WebcastPushConnection('@user');

connection.on('chat', data => console.log(data.uniqueId, data.comment));

connection.connect().catch(err => console.error(err));

...And see if the problem also occurs with that code.

@vladosnik
Copy link
Author

Ok i will try it

@vladosnik
Copy link
Author

vladosnik commented Sep 4, 2022

OK i just tested, looks like it's working fine. I am sorry about that. Something wrong with my code or may be my eyes. I am very sorry that i took your time on that. Looks like i need some kind of array that storing messages and just sending them to client one after another with little delay, or on the client side receive messages and show them with little delay, because if tiktok sends alot of messages it's like many at same time and looks like showing up the last one so problem with my application and the way i am rendering messages, it's not your issue, i am very sorry. Thank you for fast feedback. Love your npm tiktok-live-connector you are real hacker. Very talanted! Best wishes!

@vladosnik vladosnik changed the title Chat messages skipping Chat messages skipping(title does not match the issue, everything works well) Sep 4, 2022
@vladosnik vladosnik changed the title Chat messages skipping(title does not match the issue, everything works well) (title does not match the issue, everything works well)Chat messages skipping Sep 4, 2022
@vladosnik vladosnik changed the title (title does not match the issue, everything works well)Chat messages skipping (title does not match the issue, everything works well)Chat messages Sep 4, 2022
@vladosnik vladosnik changed the title (title does not match the issue, everything works well)Chat messages Chat messages works well Sep 4, 2022
@vladosnik
Copy link
Author

I fixed my code, it works nice, solution was too simple, i created array of messages and each time message come i just write it to array, and then i use setInterval(render, 100) and each 100 ms the first message of array showing up and removing from array so if somebody needs this kind of functionality this is how i made it

@vladosnik
Copy link
Author

vladosnik commented Sep 4, 2022

Thank you author for such amazing library, 9999.... likes to you

@zerodytrash
Copy link
Owner

Thanks! Good to hear that everything is working now :)

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