简体   繁体   中英

Python socket.io client unable to emit event to server(node.js) after hosting on Heroku

I have tested the code on localhost and it works. However, when i push it to heroku, the code does not emit event.

I am using this library for python client side: https://python-socketio.readthedocs.io/en/latest/client.html#installation

This is my code: Server: (I host this code on glitch)

onst express = require('express') // to host web server
const socketIO = require('socket.io')// to provide us the realtime communication
const cors = require('cors')

const port = process.env.PORT 
// const port = process.env.PORT || 80
const app = express()
app.use(cors())

const server = require('http').createServer(app).listen(port)
const io = socketIO.listen(server,{
  pingInterval: 10000,
  pingTimeout: 5000,
})
io.set('origins', '*:*')

console.log('Server has started...')

app.get('/', function (req, res) {
  res.sendStatus(200)
})

// Whenever a socket/user joins the server (io),
io.on('connection', (socket) => {
  console.log(socket.id)
  socket.on('online',()=>{
    console.log('online')
    //do nothing
  })
  socket.on('NEW_ORDER', (data,id)=>{
    console.log('receive order',data)
    console.log(data['order'])
    
  })
}

Client side:(I host these code on heroku)

import socketio

sio = socketio.Client()

@hawker_centers_blueprint.route('/payment_successful', methods=['POST'])
def pymt_succesful():
  print('connected to socket', sio.sid) #this shows on heroku log and i can know the socket's id
  sio.emit('online')#this does not show on server side's log

  sio.emit('NEW_ORDER',{'order':order, 'eatery_id':order['eatery_id']}) #This does not show on server side's log
  print('emit order')# this shows on heroku's log

sio.connect('https://magenta-buttery-silver.glitch.me/')

When i check the log, i know that my python client is connected to socket server.It is either the server does not react when python client emit an event or the python client does not emit an event.

Can anyone help me with this?

I have solved it by doing this: Moving these 2 line of code inside def pymt_succesful():

  1. sio = socketio.Client()
  2. sio.connect('https://magenta-buttery-silver.glitch.me/')

Result client side:

@hawker_centers_blueprint.route('/payment_successful', methods=['POST'])
def pymt_succesful():
    sio = socketio.Client()
    sio.connect('https://magenta-buttery-silver.glitch.me/')

    print('connected to socket', sio.sid)
    sio.emit('online')

    sio.emit('NEW_ORDER',{'order':order, 'eatery_id':order['eatery_id']}) 
    print('emit order')

Here comes another problem: Everytime a new user access this client url, socket server will create a new socket but it will not disconnect. In other words, it will have a lot of socket id that will not disconnect as this client is hosted on heroku and heroku is always online. I am afraid that it will cause a trouble to heroku and socket server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM