I'm writing a web app in Node.js and socket.io. I have a couple of button listeners that pick up when a user has clicked a button and then performs a specific action (in this case, playing a small tone). For some reason, these buttons work perfectly in Chrome/Chromium, but don't work in Firefox or on mobile browsers (I've tested it with Safari on iOS and Chrome on Android).
Here's my code:
var socket = io.connect('http://localhost:5000');
var long = document.getElementById('long');
short = document.getElementById('short');
long.addEventListener('click', function() {
socket.emit('long', socket.id);
socket.broadcast.emit('audio file', {
audio: true,
audioBuffer: buf
});
});
short.addEventListener('click', function() {
socket.emit('short', socket.id);
socket.broadcast.emit('audio file', {
audio: true,
audioBuffer: buf2
});
});
socket.on('long', function(data) {
socket.broadcast.emit('audio file', {
audio: true,
audioBuffer: longbuf
});
console.log("Transmitting", socket.id);
});
socket.on('short', function(data) {
socket.broadcast.emit('audio file', {
audio: true,
audioBuffer: shortbuf
});
console.log("Transmitting", socket.id);
});
server.js
var express = require('express');
var socket = require('socket.io');
var fs = require('fs');
var app = express();
var server = app.listen(5000, function() {
console.log("Listening to requests on port 5000")
});
app.use(express.static('public'));
var io = socket(server);
io.on('connection', function(socket) {
console.log('New transmitter', socket.id);
fs.readFile(__dirname + '/public/sounds/trollism.wav', function(err,
longbuf) {
socket.on('long', function(data) {
socket.broadcast.emit('long', data);
console.log("Long press from " + socket.id);
socket.broadcast.emit('audio file', {
audio: true,
audioBuffer: longbuf
});
});
});
fs.readFile(__dirname + '/public/sounds/trollism2.wav',
function(err, shortbuf) {
socket.on('short', function(data) {
socket.broadcast.emit('long', data);
console.log("Short press from " + socket.id);
socket.broadcast.emit('audio file', {
audio: true,
audioBuffer: shortbuf
});
});
});
});
Thanks for any help.
Edit: Decided to go jfriend00's route and make the client call the server via socket.emit(). Thanks for your help!
For starters, socket.broadcast.emit()
is not a client-side method. That's something that is available on the server. A client sends ONLY to the server with socket.emit()
.
A client doesn't send messages directly to other clients - that's not how socket.io works. socket.io is a connection from your client to your server. While the method may work on some platforms and send a message to your server, it is not documented and should not be used on any client platform.
Perhaps the iOS implementation either does nothing or has some sort of internal error when you attempt to use the undocumented socket.broadcast.emit()
. You should be using socket.emit()
instead.
If you want to relay messages to other clients from a client, you create your own message that you send to the server for that activity and then when the server receives that message, the server can then broadcast to other clients.