Here is what postman returns when I try making socket connections
I'm trying to do real time messaging using flask-socketio. when I try to make socket connections I get the error bellow.
Error:{error}
Handshake Details
Request URL: http://ec2-18-234-207-91.compute-1.amazonaws.com/socket.io/?EIO=4&transport=websocket
Request Method: GET
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: +6WOYDKiGdr5DMv3EihFHQ==
Connection: Upgrade
Upgrade: websocket
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2ODc1NDAxODIsImlhdCI6MTY4NDk0ODE4Miwic3ViIjozNjk1LCJzdHJpbmciOiI1NzAySkw2SFkifQ.L7CBLJClIBFT9ZF6gJ2rQAl5gmMU4FG8sWpWm1woxzk
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: ec2-18-234-207-91.compute-1.amazonaws.com
Here is a sample function that I'm using on a file send_message.py:
def emit_message(locator, message):
sent_message = {
"message_id": message["message_id"],
"text": message["message"],
"createdAt": message["timestamp"],
"sender": message["sender"],
"receiver": message["receiver"],
"user": {
"locator": message["sender"],
"name": message["display_name"],
"avatar": message["profile_image"],
},
"image": 0,
# You can also add a video prop:
"video": 0,
# Mark the message as sent, using one tick
"sent": 1,
# Mark the message as received, using two tick
"received": 1,
# Mark the message as pending with a clock loader
"pending": 0,
# Any additional custom parameters are passed through
"total": 0,
"statusCode": 200
}
socketio = app.config['socket']
room = get_user_room.get(locator)
socketio.emit('receiveMessage', sent_message, room=room)
here is where I call the function on a file named server.py:
import flask
from flask_socketio import SocketIO, emit, join_room
from messages import send_message
app = flask.Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*", logger = True, engineio_logger= True) # ,async_mode="gevent_uwsgi"
app.config["socket"] = socketio
@socketio.on("tena")
def socket_op(message):
header = request.headers.get("Authorization")
_id = request.sid
subject = 0
msg_received = 0
try:
msg_received = message
subject = msg_received["subject"]
except Exception:
pass
if subject == "sendMessage":
return send_message.send(header, msg_received)
if __name__ == "__main__":
socketio.run(app, host="0.0.0.0", port=5000, debug=True, allow_unsafe_werkzeug=True)