Answer the question
In order to leave comments, you need to log in
How to establish a connection using socket.io-client with another site's API?
Good day. I solve the problem of parsing data from the api of a third-party site.
Nodejs environment. I need to do it through socket.io-client, I didn’t work with websockets before, so now I’m stuck on one problem and I can’t move on. I read a bunch of materials, but more and more questions arise.
There is a site on which info similar to a chat is updated in the html container, but not a chat, in devtools through the Network (WS) tab I see the address of the web-socket, and I also see the actual updated data. I need to output this data to my console.
I'm trying to establish a connection as shown in the socket.io-client documentation:
const io = require('socket.io-client');
const socket = io('wss://cf2-bot.site.org/socket.io/?EIO=3&transport=websocket&sid=825f4177-8e0d-4f29-b3b1-ce66a2adbea5');
socket.on('connect', () => {console.log(socket.id)});
Answer the question
In order to leave comments, you need to log in
It's decided. In socket.io-client, you need to specify the main web socket server, and pass parameters through options.
It is important to specify the transport (long-polling by default).
My mistake was that I passed the request directly to the uri.
const url = 'wss://sf2-site.ru';
const socket = io(url, {
query: {
path: '/socket.io',
transports: ['websocket'],
origins: 'https://www.site.org'
}
});
socket.on("connect", (data) => {
console.log('connected');
});
The documentation for the send function https://github.com/websockets/ws/blob/HEAD/doc/ws....
has a description of all parameters - name, options, callback
callback is a function in whose parameters the response just arrives.
ws.send('NAME_GET', null, function (result) {
console.log(result);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question