How to trigger messages using socket.io?
html, javascript, node.js, socket.io
Solution
`on` is used when you want to listen to an event that is sent from the server, if you want to send a message you should use `emit` instead:
socket.emit('message', { my: 'data' });
Problem
How is it possible to send a message using a button in html5? here is my code: server: ``` var io = require('socket.io').listen(72); io.on('connection', function(client){ client.send('{"success": 1}'); client.on('message', function(data) { console.log('Client:', data); }); client.on('disconnect', function() { console.log('Goodbye'); }); }); ``` client: ``` <!DOCTYPE html> <html> <head> <script src="http://192.168.0.102:72/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://192.168.0.102:72'); function doit(){ socket.on('message', function(data) { socket.send("hi there"); }); } </script> </head> <body> <button name="Klickme" type="button" onclick="doit();">On!</button> <button id="off" type="button" onclick="close();">Off!</button> </body> </html> ``` I get no error on my client/server, but I don´t get the message either. Can Somebody give me a hint? thanks!!!