How do I implement a pub / sub socket.io client in python?
I'm trying to implement the python equivalent of the code below, but I can't seem to get it to work.
var io = require('socket.io-client')
var socket = io('http://devel.hz.udoidio.info:5000')
socket.on('connect', function () {
socket.emit('subscribe', 'new-tx')
})
socket.on('new-tx', function (txid) {
console.log('New tx: ' + txid)
})
I tried this approach but it gives nothing.
from socketIO_client import SocketIO, LoggingNamespace
def on_response(*args):
print 'on_response', args
baseurl = "http://v1.livenet.bitcoin.chromanode.net"
socketIO = SocketIO(baseurl, 80, LoggingNamespace)
socketIO.on('subscribe', on_response)
socketIO.emit('subscribe', 'new-block')
socketIO.wait()
+3
source to share
1 answer
I solved the problem, below is the correct solution.
from socketIO_client import SocketIO
def on_response(*args):
print 'on_response', args
baseurl = "http://devel.hz.udoidio.info"
with SocketIO(baseurl, 5000) as socketIO:
socketIO.on('new-tx', on_response)
socketIO.emit('subscribe', 'new-tx')
socketIO.wait()
+2
source to share