Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda