I'm attempting to create a one to one chat using Sails.js and sails.io.js on the client side.
I can get the io.socket.get
and io.socket.post
to work, but I haven't been able to receive anything from either sails.sockets.broadcast
or Model.publish
as instructed here:
Personalized chat using Sails.js
or here
Sails.js + socket.io: Sending messages from server to clients
Server Side Code:
UserController.js
module.exports = {
listen: function(req, res) {
console.log("about to join " + userId);
sails.sockets.join(req.socket, req.param('userId'));
}
};
From: http://beta.sailsjs.org/#/documentation/reference/sails.sockets/sails.sockets.join.html
MessageController.js
// Some code to get userId and message model here
console.log("about to broadcast on " + userId);
sails.sockets.broadcast(userId, 'conversation_message', message);
// Did not JSONify the message model, not sure if I need to?
From: http://beta.sailsjs.org/#/documentation/reference/sails.sockets/sails.sockets.broadcast.html
Client Side Code:
// Some code to get userId...
io.socket.get('/user/listen', {
userId: userId
}, function() {
io.socket.on('conversation_message', function(message) {
console.log("we have a message");
});
});
When I hit the route that triggers the broadcast with userId
, nothing is sent to this client code to log we have a message
. Any ideas?
I have tried the Model.subscribe / Model.publish methods as well with no luck--same issue.
Update: The listen
function doesn't actually return -- as someone suggested in a comment that they later deleted. Adding res.send(200);
to the end of the listen
function was enough to make it work.
If that person would add their comment again as an answer I'll accept it.
(That was me. I thought my comment might of been not worthwhile because i reread your post and you said you had the get working so I thought I was wrong. My comment was something to the effect below, but I don't remember verbatim)
Have you checked to make sure the you are actually getting a response on the client-side listen? What log statements are you actually getting throughout your code? Put a console.log('get user listen') or something inside of the Client-side socket.get.