Reputation: 1183
I have a server-client connection protocol established as such:
Server:
const app = express();
const http = require('http');
const server = http.createServer(app);
const io = require('socket.io').listen(server);
io.on('connection', function (socket) {
"use strict";
socket.emit('connectionSuccess', {});
socket.emit('initmap', map.objects);
io.on('test', function (event) {
console.log('received client emit');
});
});
Client:
socket.on('connectionSuccess', function (event) {
console.log('connection successful');
socket.emit('test', {"test": "data"});
});
When the server runs on a specified port, and a user accesses the client, a connection is established and the client console outputs a connection success.
The server is able to emit 'initmap' to send data to the client for it to be read, however, the same can't be said for sending test data from the client back to the server upon connection.
Why is it that emits seem to only work from server to client and not client to server in this instance?
Upvotes: 1
Views: 456
Reputation: 203534
You should listen for test
events on the socket instance, not on the server instance:
io.on('connection', function (socket) {
...
socket.on('test', function (event) { // <-- `socket.on()` instead of `io.on()`
console.log('received client emit');
});
});
Upvotes: 2