last day (17 days later) » 

3:50 PM
1
Q: Is there any way to connect to SE Chat's WebSocket or access the socket response object in browser console?

VikasI'm writing a script to send messages using a browser console for an SE Chat bot. Now, I need the user name and timestamp of each message. I found that it is sent in WebSockets. But the only global variable I can access in the console is CHAT. But it doesn't contain all the latest data. So, is th...

 
@hyper-neutrino updated in question
@Glorfindel so technically it should be able to connect? Earlier I thought it might not be connecting because we're creating another socket connection so it might be possible stackexchange don't allow that.
 
did you authenticate with your fkey?
 
@rene I don't know about the key. Is there any key also?
 
yes, every call in chat (and on the main site as well) require an fkey. it is normally pushed in the html payload (input field name 'fkey') $('input[name="fkey"]')[0].value
 
@rene thanks. I can access it. But I had another doubt. Is it allowed to create another socket on same domain? Won't stackexchange get slow if I create another socket when it is already created?
 
3:50 PM
I don't think Nick will notice if in the 209,420,973 connections made each day, you'll add one ...
 
@rene :P one last question related to this. Any way we can access the response object/json itself without connecting, in browser console? I have access to global variable CHAT but it doesn't have all data. If I can access, I won't need to connect to socket
 
I haven't found a way to intercept websocket messages of already existing sockets. But last time I checked was back in 2015 or so. Maybe things have changed for the worst over time.
 
4:05 PM
@Vikas what's up?
 
@rene I actually wanted to ask more but I thought you'd be busy
Right now I've written ajax jquery function to create auth to get socket url
I don't what is roomid = 1
do all rooms have id 1?
 
no, the room id of this room, is 1591
 
@rene right now I'm getting same error, I made post request succesfullly and recieved the wss url
Connection closed before receiving a handshake response
 
you're doing a userscript, right?
 
@rene Javascript ajax
I think I'm doing something wrong.
 
4:17 PM
@Vikas in the browser?
 
@rene yes
ws-auth request was successful
I even received unique wss url too
 
where did you get your &l= value from?
this bit ?l=125410106
 
@rene means?
&l means?
 
that last bit after the ? is the time
I don't think 125410106 is correct for today
i expect 1623255797754 maybe divided by 1000
 
@rene oh please ignore that. I had ealier copied it from browser network tab directly. Now I'm using the exact url sent by POST request only
@rene THis is my new url = wss://chat.sockets.stackexchange.com/events/1/53ebfe938*********ece44be5a73
there's no &l
 
4:26 PM
You can't ignore it. It needs to be the value you get from a post to /events, that gives you a time property, its value needs to be added as ?l={time}
 
@rene you mean I need to make a post request to /events?
 
yeah
 
@rene I get it now, I forgot to check my network tab, there' one POST request to events like you said. I'll write code for that too
 
step 1: https://glitch.com/edit/#!/mse-so-se-chatbot?path=server%2Fse-chatroom.js%3A266%3A17
step 2: https://glitch.com/edit/#!/mse-so-se-chatbot?path=server%2Fse-chatroom.js%3A268%3A19
step 3: https://glitch.com/edit/#!/mse-so-se-chatbot?path=server%2Fse-chatroom.js%3A152%3A29
step 4: https://glitch.com/edit/#!/mse-so-se-chatbot?path=server%2Fse-chatroom.js%3A164%3A48
 
do I need to send all these fields if you know? since=0&mode=Messages&msgCount=100&fkey=**************************

I know key is necessary
@rene this is fo rme?
nevermind, I think it is going forward
 
4:39 PM
@Vikas it is an example of working code that connects to a chat websocket. I don't have time to bring it down to an MCVE for you.
@Vikas yes, you need all, you can set msgCount=1 as you're not really interested in many events
 
@rene I just sent fkey and it worked. But empty events. Does it mean I wont get any data in socket later?
@rene Lo! it's it's open now.
But I didn't even sent all those parameters. Will there any problems?
since, mode, msgcount ignored
 
I dunno, maybe you're lucky ;)
 
@rene I'm not receiving any events
could it be because of that?
 
are you receiving heartbeats?
 
@rene heartbeats? I'm seeing results in Network tab socket response though
 
4:48 PM
okay, that works then
hook up your event listeners
 
@rene onmessage? I've already done it, but not seeing anything in console
that's strange because i can see latest data in network tab
i'll cross check again
 
aWebSocket.onmessage = (msg) => console.log; should work
 
@rene Oops. Same problem. It is connected successfully but still no console.log results in onmessage
yes applied same
@rene sorry my bad :D
@rene by default it was console.debug
but still i've one last doubt. what was the purpose of those 3 extra parameters?
It look slike working without even these
 
yeah, I use them to populate an seen users collection. I guess you don't need it if you're not interested in the latest messages / users.
So, it works then?
We're all good?
 
@rene makes sense.
@rene I did one more testing. I closed it manually to troubleshoot
So I'll need to get timestamp again? Beause I can't connect using same timestamp
Yes. It needs to be posted again. I can connect using fresh timestamp
Thank you so much for your patience :)
 
5:07 PM
you're welcome. Take care!
 

  last day (17 days later) »