I am trying to make a game server with node.js
, socket.io
.
The basic idea likes below.
- Initialize
socket.io
instance when the server starts - Store instance in global scope, so controllers can access it
- When API calls, we trigger some
socket.io
event in the controller or some other points
Here is the implementation I made ...
First, in server.js
- entry point
let GlobalVars = require('./state/GlobalVars');
const apiRouters = require('./router');
...
app.use('/api', apiRouters);
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/test/simpleClient.html`)
});
const httpServer = http.createServer(app);
let socketIOInstance = socketIO(httpServer);
socketIOInstance.on('connection', (socket) => {
console.log('SOCKET.IO A USER CONNECTED');
socket.on('create', (data) => {
console.log('SOCKET.IO create called', socket);
socket.join(data.room);
socketIOInstance.emit('message', 'New people joined');
});
socket.on('join', (data) => {
console.log('SOCKET.IO join called', data);
})
socket.emit('message', 'Hi');
});
GlobalVars.socketIO = socketIOInstance;
// Add to global, so the controllers can manage own actions like create, join ...
httpServer.listen(port, () => {
console.log(`Server Listening on the port ${port}`);
})
...
When I access from a client, I am able to see SOCKET.IO A USER CONNECTED
and Hi
in the browser console.
Second, In api controller.
let GlobalVars = require('../state/GlobalVars');
...
router.post('/create', (req, res) => {
console.log('GenerateGameSokect');
let game = new Game();
let gameId = game.gameId;
// console.log('Global vars ', GlobalVars.socketIO);
GlobalVars.socketIO.emit('create', {
room: gameId
});
res.json({
result : 'SUCCESS',
game : game
})
});
I imported GlobalVars
which contains socketIO instance. So what I expected was, socket create
event triggered from the statement GlobalVars.socketIO.emit('create', Object)
but could not find message in the server logs.
I got no clue what I was missing.
The final form I pursue is something like...
- When user call create API, I creates socket connection and room
- API will called in HTTP protocol, but in the API, the server publishes some events. - pubsub like.
Thanks for reading my questions b. Here is full source code till now(bitbucket public)
================== EDIT ====================
I got understood (maybe...)
The user-flow I wanted was ...
- The client call API
- (In the server) Checking validation in API and if valid emit to
socket.io
- If event accepted send new status to all clients
However, creating socket.io
connection in the server looks strange for me, the solution is up to the client.
New user-flow I will change
- The client call a validation API
- If return is valid, the client emit socket.io event. This time server only do validation, not emit socket.io
- In socket event, send new status to all other users
================== EDIT #2 ====================
This is a kind of conclusion. It looks I just misunderstanding the concept of socket communication. Like answer and replies say, Socket
and HTTP
are totally different channels, there is no way to connect both. (At least, without open new connection from http server to socket)
If this is wrong, you could add reply, Thanks