Skip to main content

Socket.io usage

Below is a simple example of how to get started with socket.io within Routine

const Routine = require('routinejs')
const { Server } = require('socket.io')

const app = new Routine({
enableBodyParsing: true,
catchErrors: true,
})

//app.listen returns the server instance
const server = app.listen(8080)

//using that server instance to create a socket server
var io = new Server(server, {
cors: {
//adding cors for the online socket io testing tool
origin: 'https://amritb.github.io',
methods: ['GET', 'POST'],
},
})

//performing socket io connection and events
io.on('connection', (socket) => {
console.log('a user connected')
socket.on('hello', (data) => {
console.log(data)
socket.emit('chat', data)
io.emit('chat', data)
})
})