Following the documentation on the SocketIO website, I have the following code:
Server
socket.on('foo', (arg, ack) => { //Do stuff with arg if(ack) ack('response'); });
Client
socket.emit('foo', arg, (response) => { console.log(response); });
However, the ack
function is never called. As a matter of fact, it is undefined
. I am using SocketIO v2.0.4 on both server and client side.
Am I missing something? The docs make it look like it should be that easy, yet I just can't figure it out!
Thanks!
3 Answers
Answers 1
Server
socket.on('foo', (arg) => { //Do stuff with arg console.log('message from client : ' + data) socket.emit('bar','message acknowledge from server'); });
Client
socket.emit('foo', 'mymessage'); socket.on('bar', (data) => { console.log(data) });
using socket.io
in server side and socket.io-client
in client side
Server side
var app = require('http').createServer(); var io = require('socket.io')(app); app.listen(80); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }, function(res) { console.log(res); }); });
Client side
var io = require('socket.io-client'); var socket = io('http://localhost'); socket.on('news', function (data, ack) { console.log(data); if(ack){ ack("acknowledge from client"); } });
Answers 2
Server side
var io = require('socket.io')(8090); io.on('connection', function (socket) { console.log('connected') socket.on('ferret', function (arg, ack) { console.log('ferret') ack('woot'); }); });
Client side
const ioClient = require('socket.io-client'); var client = ioClient.connect('http://localhost:8090'); client.emit('ferret', 'tobu', (response) => { console.log(response) console.log('ack') });
It's will log the response of ack() and the 'ack' string. i got the reference socket.io acknowledge node.js sample. hope it helps.
Answers 3
This issue usually occurs when you have a mismatch between the socket.io
version and socket.io.js
version. That is one scenario in which I was able to reproduce the ack
being undefined.
Other I just tested it with Socket 2.04 and it works fine.
index.js
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var port = process.env.PORT || 3000; var host = process.env.HOST || "0.0.0.0"; app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); app.get('*', function (req, res) { console.log(' request received ', req.headers.host); res.status(200).send(); }); io.on('connection', function(socket){ socket.on('chat message', function(msg){ io.emit('chat message', msg); }); socket.on('ferret', function (name, fn) { console.log('name', name, 'fn', fn); fn('tarun'); }); }); http.listen(port,host, function(){ console.log('listening on *:' + port); console.log('Env name is ' + process.env.name) });
index.html
<!doctype html> <html> <head> <title>Socket.IO chat</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font: 13px Helvetica, Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } #messages { list-style-type: none; margin: 0; padding: 0; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } #messages { margin-bottom: 40px } </style> </head> <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> <script src="/socket.io/socket.io.js"></script> <script src="https://code.jquery.com/jquery-1.11.1.js"></script> <script> $(function () { var socket = io(); socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too! socket.emit('ferret', 'tobi', function (data) { console.log(data); // data will be 'woot' }); }); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); window.scrollTo(0, document.body.scrollHeight); }); }); </script> </body> </html>
package.json
{ "name": "socket-chat-example", "version": "0.0.1", "description": "my first socket.io app", "dependencies": { "express": "^4.15.2", "socket.io": "^2.0.4" }, "scripts": { "start": "node index.js" } }
0 comments:
Post a Comment