Closed Nykke closed 7 years ago
It seems that the issue is here:
//send chat messages
var socket = io;
socket.on("send message", function(data){
io.sockets.emit("new message", {msg: data, user: socket.username});
console.log(data)
})
//send chat messages
///move your emit somewhere outside of the socket listener. the event names should match also
io.sockets.emit("message", {msg: data, user: socket.username});
var socket = io;
socket.on("message", function(data){
console.log(data)
})
just tried doing that and the form is still not taking the input.
Do you this line inside the form submission handling function?
io.sockets.emit("message", {msg: data, user: socket.username});
yes, I have a similar line
$messageForm.submit(function(e){
e.preventDefault()
console.log(submitted)
socket.emit("send message", $message.val())
$message.val(" ")
})
change socket.emit("send message")
to socket.emit("message")
. the first argument to emit
and on
is a string naming the event type (the strings must match in order for on
to listen for the same event being emit
ted). "send message"
is therefore assumed to be a different type of user-defined event than "message"
or "new message"
.
it didn't work, sorry I don't understand what's going
emit("message", /*callback function here*/)
emits an event that will trigger the callback inside of on("message", , /*callback function here*/)
would you mind taking a look at my branch? I get no errors and it's just not taking the value https://github.com/Nykke/project_04/tree/websocket
what value do you mean?
the input that the user is entering into the form
it's because this line of code is stranded outside the route that handles form submissions
io.sockets.emit("message", {msg: data, chatter: socket.username});
you'll have to adapt it though to the data available in available in your post handler:
app.post("/api/maintenance_requests", function(req, res){
Maintenance_Request.create(req.body).then(function(maintenance_request){
io.sockets.emit("message", {msg: maintenance_request});
res.json(maintenance_request);
});
})
I'm calling it quits, it didn't work. thanks for your help
I'm not getting anything in my server (my console logs) and I'm currently working on making it angular readable but shouldn't I be able to create my messages as is?
this is index.js