Created
June 29, 2022 20:37
-
-
Save exvion/cfe1a29375003b4326848bda38ecad72 to your computer and use it in GitHub Desktop.
uWS server with subscribe on quotes from QuikClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uWS from "uWebSockets.js"; | |
import { QuikClient } from './QuikClient.mjs' | |
const connection = new QuikClient({ | |
host: "10.211.55.3", | |
port: 34130 | |
}); | |
connection.connect(); | |
connection.on('onQuote', (data) => { | |
app.publish('onQuote', JSON.stringify(["onQuote", data])); | |
}); | |
// server | |
const app = uWS.App({}) | |
.ws('/*', { | |
maxBackpressure: 10 * 1024 * 1024, | |
open: (ws) => { | |
ws.subscribe('onQuote'); | |
}, | |
close: (ws) => { | |
ws.closed = true; | |
}, | |
drain: (ws) => { | |
if (!ws.closed) | |
return ws.send( | |
JSON.stringify([{ T: 'error', code: 407, msg: 'slow client' }]) | |
); | |
}, | |
message: async (ws, message) => { | |
if (ws.closed) return; | |
try { | |
const payload = JSON.parse(Buffer.from(message).toString()); | |
console.log(payload); | |
} catch (e) { | |
console.error(e); | |
!ws.closed && | |
ws.send( | |
JSON.stringify([{ T: 'error', code: 400, msg: 'invalid syntax' }]) | |
); | |
} | |
}, | |
}) | |
.get('/ping', async (res, req) => { | |
res | |
.writeStatus('200 OK') | |
.writeHeader('Content-Type', 'text/plain;charset=UTF-8') | |
.end('pong'); | |
}) | |
.listen(9003, (listenSocket) => { | |
if (listenSocket) { | |
console.log('Listening to port 9003'); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment