Last active
January 6, 2022 16:43
-
-
Save daffl/0647ac1a85458389c3b253a1388eb194 to your computer and use it in GitHub Desktop.
Socket.io counter acknowledgement example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const app = require('express')(); | |
const http = require('http').Server(app); | |
const io = require('socket.io')(http); | |
let counter = 0; | |
app.get('/', function(req, res){ | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
io.on('connection', function(socket){ | |
socket.on('getCounter', function(callback) { | |
// Increment the counter | |
counter++; | |
console.log(`Returning getCounter with counter ${counter}`); | |
// Use a Node style callback (error, value) | |
callback(null, counter); | |
}); | |
}); | |
http.listen(3000, function(){ | |
console.log('listening on *:3000'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io(); | |
socket.emit('getCounter', function(error, counter) { | |
console.log('Counter is', counter); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes you can