Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cristovao-trevisan/socket.io-topic-router

Express like routing path middleware for socket.io
https://github.com/cristovao-trevisan/socket.io-topic-router

expressjs middleware routing socket-io

Last synced: 23 days ago
JSON representation

Express like routing path middleware for socket.io

Awesome Lists containing this project

README

        

# socket.io-topic-router


npm version
Standard - JavaScript Style Guide



Express like routing middleware for socket.io

## Install

`npm install socket.io-topic-router`

## Usage

### Basic

#### Server
```javascript
const io = require('socket.io')(8080)
const ioRouter = require('socket.io-topic-router')()

io.use(ioRouter)
ioRouter.route('foo/:bar', (params, [arg1, arg2], next, socket) => {
console.log(params, arg1, arg2)
// params.bar === '1234'
// arg1 === 'arg1'
// arg2 === 'arg2'
socket.emit('foo/1234', 'bar')
})
```

#### Client

```javascript
const io = require('socket.io-client')

var socket = io('http://localhost:8080')
socket.on('foo/1234', resp => {
console.log(resp)
// resp === 'bar'
})
socket.emit('foo/1234', 'arg1', 'arg2')
```

### Multiple Callbacks

#### Server
```javascript
const io = require('socket.io')(8080)
const ioRouter = require('socket.io-topic-router')()

const ensureAuth = (params, args, next) => {
var {username, password} = args[0]
if (username === 'admin' && password === '1234') {
next()
} else {
next(new Error('Need authentication'))
}
}

io.use(ioRouter)
ioRouter.route('user/:id', ensureAuth, (params, args, next, socket) => {
var user = {name: 'Jack Sparrow'}
socket.emit('user/' + params.id, user)
})
```

#### Client

```javascript
const io = require('socket.io-client')

var socket1 = io('http://localhost:8080')
socket1.on('user/1', user => {
console.log('Socket 1:', user)
// user.name === 'Jack Sparrow'
})
socket1.emit('user/1', {
username: 'admin',
password: '1234'
})

var socket2 = io('http://localhost:8080')
socket2.on('user/1', user => {
// never called'
})
socket2.on('error', err => {
console.log('Socket 2:', err)
// Need authentication
})
socket2.emit('user/1', {
username: 'admin',
password: 'not this'
})
```