README
v50-socket-server
v50-socket-server uses configuration object and creates socket.io API automatically. Configuration can be created manually or loaded from v50-router
Example
// Load v50server
const v50server = require('v50-socket-server')
// Load socket.io client to connect to the server
const clientIo = require('socket.io-client')
// create router configuration object
// "test" (controller) > "gettest" (action) > Array with function to execute in order
// this example provides an example for loading one controller with one action
// You can define multiple controller and actions
// read more about creating this structure automatically in v50-router repository
const router = {
test: {
gettest: [
(res, req) => {
return res(null, {httpStatus: 200, payload: true})
}
]
}
}
// create v50 server instance
// first argument is options object. Default is {host: '127.0.0.1', port: 1337, router: {}}
// second argument is a callback function, it returns socket.io server object
v50server({port: 5666, router}, server => {
// create socket.io client connection
const socket = clientIo('http://localhost:5666')
// when socket.io client is connected execute function
socket.on('connect', () => {
// socket.io client emit message to server
// first, param "message" is namespace on which server is listening
// second, param "test" is controller defined in router configuration
// third, post param, the server will receive it in req object
// fourth, callback function with the response from the server
socket.emit('message', 'test', 'gettest', {}, result => {
console.log(result) // {httpStatus: 200, payload: true}
// close socket.io client
socket.close()
// close v50-socket-server
server.close()
})
})
})
For more examples see tests
Tests
npm test
Contributors
Edgars Mjasnikovs
License
MIT