socket io middleware
const express = require('express');
const socketio = require('socket.io');
// Setup server and socket
const port = 3000;
const app = express();
const server = app.listen(port, console.log(`Listening on port ${port}...`));
const io = socketio(server);
// Calls the middleware on every new connection
io.use((socket, next) => {
if (/* should connect socket */) {
next();
} else {
next(new Error('Connection failed.'));
}
});
io.on('connection', (socket) => {
// Calls the middleware on every event
socket.use((packet, next) => {
const event = packet[0];
const data = packet.slice(1);
if (/* should run event */) {
next();
} else {
next(new Error('Failed to emit the event.'));
}
});
// Example event
socket.on('msg', (data) => {
socket.broadcast.emit('msg', data.msg);
});
});
Also in JavaScript:
- navigation.openDrawer is not a function react native
- chart js radar grid color
- How to get the input from a textbox javascript
- javascript .target
- web worker multiple data
- space between react native
- javascript check for undefined
- how to detect a keypress in javascript
- Cannot find module '@angular/cdk/a11y'
- global variable define in javascript
- get input field inside div jquery
- javascript date
- jquery remove option from select by value
- change style on click react
- react native add custom fonts
- what is npm global install
- copy text javascript
- Javascript program printing the lyrics to the 99 bottles of beer song web
- javascript get last character of string
- remove last char string js
- vuejs show {{}} on loading
- document is not defined javascript in nuxt js
- change array range value javascript
- inline z-index react