Last active
June 5, 2024 11:18
-
-
Save mbaka-bilal/c1a38c61450cb5444870be26fbd3c962 to your computer and use it in GitHub Desktop.
smart change socket.io server not emitting events back to client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is the chat model that is been used to test | |
{ | |
"status": true, | |
"chat": { | |
"_id": "665317595782332ba2ce6d5a", | |
"user": "66224c1282d7be208a239fb6", | |
"merchant": "6651fa0606acda67b9ec699d", | |
"deleted": false, | |
"createdAt": "2024-05-26T11:04:57.572Z", | |
"updatedAt": "2024-05-26T11:04:57.572Z", | |
"__v": 0 | |
} | |
} | |
//conversation-opened event data | |
{conversationId: 665317595782332ba2ce6d5a} | |
//typing event data | |
{receiverHash: 1c3ac57b89, senderHash: a13d74e1cf, conversationId: 665317595782332ba2ce6d5a} | |
//new message event data | |
{receiverHash: 1c3ac57b89, senderHash: a13d74e1cf, message: Send it now i am waiting, role: user, conversationId: 665317595782332ba2ce6d5a} | |
///From the documentation this is the algorithm | |
1. when a user taps on a merchant, hit the /chat/check/ endpoint passing in the merchant id, | |
which checks if a conversation exists, creates one if it does not exists and otherwise returns existing. | |
2. connect to the socket | |
3. onsuccessfull connection send "conversation-opened" event to the server, passing in the json { | |
"conversationId": "///id gotten from step 1" | |
} | |
this event creates a room and adds the user and merchant to it. | |
4. once step 4 is successfull and only if step 4 is successfull then can we begin to send more messages to the socket, such | |
as 'typing' and 'newMessage' | |
//Problems | |
1. 'conversation-opened' emits nothing back to the client (me) after sending this event to the server so i cannot know, | |
if it was successfull or not. | |
2. if i assume step 1 was successfull (which i have done, so i could try out other events), when i send typing event to the | |
server, it emits nothing. | |
3. if i still assume 'conversation-opened' event did create the room and add the user and merchant to it, and send 'newMessage' | |
event then the server closes and throws error 'WebSocketException: Connection to 'https://smartchange-api.onrender.com:0/socket.io/?EIO=4&transport=websocket#' was not upgraded to websocket' | |
///////////////////////////////////////////////////////////// ** code | |
///Emit events to server | |
///Connect to chat socket and listen for events from server | |
void connectToChatSocket() async { | |
try { | |
final token = | |
await LocalStorage.getFromStorage(key: accessTokenKey, type: String); | |
socket = IO.io("https://smartchange-api.onrender.com/", <String, dynamic>{ | |
'transports': ['websocket'], | |
'autoConnect': false, | |
"auth": {'token': token} | |
}); | |
//connect | |
socket!.connect(); | |
socket!.on("connecting", (event) { | |
logger.d("connecting to the socket"); | |
}); | |
//This connects successfully. | |
socket!.on('connect', (_) { | |
logger.d('Connected successfully'); | |
Future.delayed(const Duration(seconds: 1), () { | |
//Send this event, as documentation says, this adds user and merchant to a room and we should make sure | |
//we send this event before sending anyother event, here we send this event immediately after a successfull | |
//connection to the server. | |
//BUT.... | |
//The server emits nothing back, so we have no idea if it was successfull or not. | |
//assuming it was successfull, let's proceed. (continue...) | |
socket.emit("conversation-opened", { | |
"conversationId": "665317595782332ba2ce6d5a" //this is a real conversation id created for the logged in user and merchant by hitting the endpoint /chat/check/ | |
//below is the json response from the server. | |
}); | |
/* | |
{ | |
"status": true, | |
"chat": { | |
"_id": "665317595782332ba2ce6d5a", | |
"user": "66224c1282d7be208a239fb6", | |
"merchant": "6651fa0606acda67b9ec699d", | |
"deleted": false, | |
"createdAt": "2024-05-26T11:04:57.572Z", | |
"updatedAt": "2024-05-26T11:04:57.572Z", | |
"__v": 0 | |
} | |
} | |
*/ | |
}); | |
}); | |
//socket io built in events | |
socket!.on('error', (e) { | |
logger.e("Error occurred $e"); | |
socket!.disconnect(); | |
socket!.close(); | |
_controller.close(); | |
}); | |
socket!.on('disconnect', (_) { | |
logger.d('Disconnected the connection'); | |
}); | |
socket!.on('connect_error', (e) { | |
logger.e('connect error: $e'); | |
logger.d("controller status ${_controller.hasListener}"); | |
socket?.disconnect(); | |
socket?.close(); | |
}); | |
socket!.on('connect_timeout', (e) { | |
logger.d('Connection timeout: $e'); | |
socket?.disconnect(); | |
socket?.close(); | |
}); | |
//Smart change api events | |
//Listen for "typing" event from the server | |
//Assuming the conversation-opened emitted to the server successfully created the room, | |
//even after sending "typing" event to server, nothing is emitted back from server. | |
socket!.on("typing", (e) { | |
logger.d("even is typing \n\n $e"); | |
}); | |
//This is the event sent to the server | |
/* | |
socket.emit('typing', | |
{ receiverHash: 1c3ac57b89, | |
senderHash: a13d74e1cf, | |
conversationId: 665317595782332ba2ce6d5a}); | |
*/ | |
//Also assuming the conversation-opened event sent to the server did create the room successfully | |
//this message when send the server causes the error below | |
// WebSocketException: Connection to 'https://smartchange-api.onrender.com:0/socket.io/?EIO=4&transport=websocket#' was not upgraded to websocket | |
//Listen for "newMessage" event from the server | |
socket!.on("newMessage", (e) { | |
//newMessage (newMessage) | |
logger.d("new message $e"); | |
}); | |
And this is the message sent to the server | |
/* | |
socket.emit('newMessage', | |
{receiverHash: 1c3ac57b89, | |
senderHash: a13d74e1cf, | |
message: Send it now i am waiting, | |
role: user, | |
conversationId: 665317595782332ba2ce6d5a} | |
*/ | |
//Listen for "conversation-opened" event from the server | |
socket!.on("conversation-opened", (e) { | |
logger.d("event is conversation-opened \n\n $e"); | |
}); | |
socket!.on( | |
'messageError', (errorData) => {print("message error $errorData")}); | |
} catch (e) { | |
logger.d("could not connect to socket \t\t $e"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment