0

i have a websocket client using the websocket api for javascript and my server is using socket.io

I'm trying to put a callback function like this

client js Using build in javascript WebSocket api


//im formatting the string like that because thats what socket.io needs for some reason
 var sendString = '42' + JSON.stringify(['checkLogin' , username, password])
 socket.send(sendString, function(returnData){
 console.log(returnData);
 resolve(returnData);
 })

but I keep getting an error that callback is not a function on my server-side using socket.io which looks like this

server js using socket.io library


socket.on('checkLogin', function(username, password, callback){
 console.log(username, password)
 webUser.findOne({username:username}, function(err, docs) {
 console.log(docs)
 if(docs){
 if(docs.password == password){
 callback(docs._id)
 }else{
 callback(false)
 }
 }
 if(!docs){
 callback(false)
 }
 });
})

what can i change to make this set the callback function correctly

asked Nov 12, 2019 at 0:13
10
  • Which websocket library are you using? Please post a link to the NPM page for the exact webSocket library you're using. Commented Nov 12, 2019 at 1:16
  • I'm using the websocket API built into the browser. I'm testing it would in an HTML file but for building tabris apps it's uses the same api Commented Nov 12, 2019 at 1:18
  • Server-side? You said the error is "callback is not a function" server-side so I'm asking what library you're using on the server. And, if you want help with a server-side error, you have to show us the relevant server-side code. If the error is not on your server, please correct your question. Commented Nov 12, 2019 at 1:42
  • FYI, the client-side socket.send() does not have a callback function as an argument. Commented Nov 12, 2019 at 1:44
  • so is there no way to achieve what I'm trying to do using the WebSockets API ? also I edited my question sorry about that I'm not feeling right today :( Commented Nov 12, 2019 at 1:46

1 Answer 1

1

First problem. If you're using socket.io on the server-side, you must be using a socket.io client. You can't use a webSocket client with a socket.io server. They won't connection. This is true even though socket.io is using the webSocket transport underneath. A socket.io server needs the socket.io layer on top of webSocket.

Second problem, to send data with a socket.io client, you would use either socket.send() when you are just sending raw data with no message name or you would use socket.emit() when you are sending a message name.

In your case, I would recommend you use this on the client:

 socket.emit('checkLogin', {username, password}, function(result => {
 console.log(result);
 });

And, then modify your server code like this:

socket.on('checkLogin', function(data, callback){
 let username = data.username;
 let password = data.password;
 console.log(username, password)
 webUser.findOne({username:username}, function(err, docs) {
 console.log(docs)
 if(docs){
 if(docs.password == password){
 callback(docs._id)
 }else{
 callback(false)
 }
 }
 if(!docs){
 callback(false)
 }
 });
})
answered Nov 12, 2019 at 2:05
Sign up to request clarification or add additional context in comments.

5 Comments

i can do regular things like socket.send('42' + JSON.stringify(['checkLogin' , username, password])); and it works fine just getting a callback to work would be weird at first i wasn't able to get them to connect but using this const socket = new WebSocket('ws://157.230.66.208:4343/socket.io/?EIO=3&transport=websocket','chat-protocol'); works like a charm
@NicholasHendricks - If you're only going to use webSocket functionality, then use a webSocket server, not a socket.io server. The whole point of using socket.io is to use the features that it adds above and beyond a plain webSocket.
ok I was just wondering if it was possible the whole reason I'm using the WebSocket API is that Tabris.js uses it and cant be used with socket.io-client sadly thanks for all of your help
@NicholasHendricks - If you want to program with webSocket, then change your server to a webSocket server and that will match with a webSocket client. You will be limited to the features that the standard webSocket protocol supports.
socket.io is already used for the web clients and i want to keep it that way that's why I was trying to make a solution for using pure WebSockets with the socket.io server because Tabris does not support socket.io client at all and now that I cant use callbacks or anything like that I definitely won't be using pure WebSockets unless I kind find a way to implement callbacks in some way. thanks for the help.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.