Transports / JavaScript /

SockJS

listen( [options] )

Options

const sockjsTransport = require('dop-transports').listen.sockjs
const listener = dop.listen({
    transport:sockjsTransport,
    namespace:'dop',
    port:4446,
    timeout:60
})
listener.on('connect', node => console.log('Connected!', node.token))

Using httpServer

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

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

Using ExpressJS

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

const sockjsTransport = require('dop-transports').listen.sockjs
const listener = dop.listen({httpServer:expressServer, transport:sockjsTransport});
listener.on('connect', node => console.log('Connected!', node.token))

connect( [options] )

Options

In node.js

const sockjsTransport = require('dop-transports').connect.sockjs
const server = dop.connect({transport:sockjsTransport})
server.on('connect', () => console.log('Server connected!'))

In browsers

In browsers you have to include SockJS client library manually

import sockjsTransport from 'dop-transports/connect/sockjs'
const server = dop.connect({transport:sockjsTransport, url:'ws://localhost:4445/test'})
server.on('connect', () => console.log('Server connected!'))
<script src="https://cdn.jsdelivr.net/sockjs/1/sockjs.min.js"></script>
<script src="https://unpkg.com/dop"></script>
<script src="https://unpkg.com/dop-transports@2.0.2/connect/sockjs.js"></script>
<script>
let sockjsTransport = dop.transports.connect.sockjs
let server = dop.connect({transport:sockjsTransport, url:'ws://localhost:4445/test'})
server.on('connect', () => console.log('Server connected!'))
</script>