Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
Add to Database using flask-socketio

I want to use flask_socketio to add realtime messaging function to my web. My website has a function in which messages are added to the messages table in my flask-sqlalchemy db. Now Im unable to understand how will I add a message to the database while at the same time posting it to the client server so it can instantly ne viewed, in easy words what flask-socketio is used for(instant update).The function to add messages is:

@app.route('/<int:team_id>/<int:channel_id>/<string:channel_name>' , methods=["GET","POST"])
def channel(team_id,channel_id, channel_name):
*Irrelevant code*
 form = MessageForm()
 if form.validate_on_submit():
    message = Messages(msg_cntnt=form.msg_cntnt.data,msg_file
    =form.picture.data, sender_id=current_user.id)
            message.parent_channel = _channel_name
    db.session.add(message)
    db.session.commit()    
messages = Messages.query.filter_by(parent_channel=_channel_name).all()
return render_template('team.html', _channel=_channel, team=team, channels=channels,
team_members_count=team_members_count, form=form, messages=messages)

I have tried a few ways of using flask socketio with it none worked. I also tried changing version of flask_socketio from 5.2.0 to 2 and then 4 but it didn't work. How can I achieve this? How can I add flask-socketio to it. NOTE: In my team.html I have done:

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could prioritize the realtime functionality first and saved it afterwards like this

def save_message(msg):
  groupid = group.query.filter(group.groupid == msg["roomid"]).first()
  save = message(msg["sender"],msg["content"])
  db.session.add(save)
  groupid.message.append(save)
  db.session.commit()
  
  

@socketio.on("send_message")
def send_message(message):
  print(message)
  message["sender"] = current_user.username
  emit('add_element', message, to = message["roomid"], broadcast = True)
  save_message(message)

When "send_message" event is triggered then act like a normal emit message function, but after everything is done (transmission of real time message) then call a function that saves the message

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda