J
J
Johnny Sins2020-05-11 15:32:14
Node.js
Johnny Sins, 2020-05-11 15:32:14

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)});


But as I understand it, the connection is not established, or am I doing something wrong at all? For starters, I would just like to see if I can connect to this bot (api), and then send requests and receive data. I have been familiar with Nodejs not so long ago, I will be glad for any help. Could it be that the connection is not established due to CloudFlare? And another question, in my IDE WebStorm 2019 shows that the .on method is "Unresolved function or method on()", why is that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Johny Sins, 2020-05-19
@jpeg_man

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');
    });

M
maned7, 2020-05-12
@maned7

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);
});

Not sure if it will work, maybe this function is just called when the request is completed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question