user751978
user751978

Reputation: 11

python-socketio client fails to get public data but NodeJS socket.io-client does not

I am trying to download publicly available data from https://www.paymium.com/public. The data provider gives an example on their site on how to access data using NodeJS. The following program works.

var io = require('socket.io-client');

var socket = io.connect('https://www.paymium.com/public', {
  path: '/ws/socket.io'
});

console.log('CONNECTING');

socket.on('connect', function() {
  console.log('CONNECTED');
  console.log('WAITING FOR DATA...');
});

socket.on('disconnect', function() {
  console.log('DISCONNECTED');
});

socket.on('stream', function(publicData) {
  console.log('GOT DATA:');
  console.log(publicData);
});

I implemented this "equivalent" program using python-socketio, but it does not work.

import socketio
sio = socketio.Client(logger=True, engineio_logger=True)

@sio.event
def stream(publicData):
    print('GOT DATA:')
    print(publicData)

@sio.event
def connect():
    print("CONNECTED")
    print('WAITING FO DATA...')

@sio.event
def disconnect():
    print("DISCONNECTED")

print('CONNECTING')
sio.connect('https://www.paymium.com/public', socketio_path='/ws/socket.io')
sio.wait()

What parameter, call-back or namespace declaration am I missing in my python equivalent program?

I ran both programs on macOS Catalina 10.15.4 using npm socket.io-client 2.3.0, python 3.7.6, and python-socketio 4.6.0.

Upvotes: 1

Views: 748

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67547

You haven't done a correct translation of the JavaScript client. Here is the working Python client code:

import socketio
sio = socketio.Client(logger=True, engineio_logger=True)

@sio.event(namespace='/public')
def stream(publicData):
    print('GOT DATA:')
    print(publicData)

@sio.event(namespace='/public')
def connect():
    print("CONNECTED")
    print('WAITING FO DATA...')

@sio.event(namespace='/public')
def disconnect():
    print("DISCONNECTED")

print('CONNECTING')
sio.connect('https://www.paymium.com', namespaces=['/public'], socketio_path='/ws/socket.io')
sio.wait()

The main difference between the JS and Python clients is in how namespaces are implemented.

Upvotes: 1

Related Questions