Transports / JavaScript /

WebSockets

listen( [options] )

Options

Using ws

const dop = require('dop')
const listener = dop.listen()

Equivalent to

const dop = require('dop')
const wsTransport = require('dop-transports').listen.ws
const listener = dop.listen({transport:wsTransport, port:4444, timeout:60})

Using uWebSockets

const uwsTransport = require('dop-transports').listen.uws
const listener = dop.listen({transport:uwsTransport})

Using httpServer

const http = require('http');
const httpServer = http.createServer(function (req, res) {})
httpServer.listen(4444);

const listener = dop.listen({httpServer:httpServer});
listener.on('connect', onconnect);
// In browser or in other node instance. Even here would work
dop.connect({url:'ws://localhost:4444'})

Using ExpressJS

const express = require('express')
const http = require('http')
const app = express()
const expressServer = http.createServer(app)
expressServer.listen(4444)

const listener = dop.listen({httpServer:expressServer});
listener.on('connect', onconnect);
// In browser or in other node instance. Even here would work
dop.connect({url:'ws://localhost:4444'})

connect( [options] )

Options

In node.js

Uses ws by default

const dop = require('dop')
const server = dop.connect()

Equivalent to

const dop = require('dop')
const wsTransport = require('dop-transports').connect.ws
const server = dop.connect({url:'ws://localhost:4444', transport:wsTransport})

In browsers

Uses native WebSockets

import dop from 'dop'
const server = dop.connect({url:'ws://localhost:4444'})
server.on('connect', ()=>{
    console.log('Connected!!')
})

If you don't use ES6

<script src="https://unpkg.com/dop"></script>
<script>
var server = dop.connect({url:'ws://localhost:4444'})
server.on('connect', function() {
    console.log('Connected!!')
})
</script>