WebReflection / coincident

An Atomics based Proxy to simplify, and synchronize, Worker related tasks.
MIT License
201 stars 3 forks source link

How we can expose server methods to main UI #36

Closed ansarizafar closed 10 months ago

ansarizafar commented 10 months ago

I am trying to use coincident but I am not able to expose server methods to main UI. Here is my code in Imba lang

# Server code
import index from './index.html'
import {createServer} from 'http'
import {WebSocketServer} from 'ws'
import coincident from 'coincident/server'

const port = process.env.PORT or 3000
const headers = 
    "Content-Type": "text/html"

const server = createServer do(req, res)
    if req.url == '/'
        res.writeHead 200, headers
        res.write index.body
        res.end!

const methods = 
    require: require
    import: do(name) import(name)

const websocket = new WebSocketServer {server}
websocket.on 'connection', do(ws) ws.send("Hello World")
coincident websocket, methods

imba.serve server.listen(port)

Client code

import coincident from 'coincident/server'

const w = new Worker('./worker.js', {type: 'module'})
const ws = new WebSocket('ws://localhost:3000/ws')
const {proxy} = coincident(w, ws)
const os = await proxy.import('os')
console.log(os.platform())

Worker code

import coincident from 'coincident/server'

let {proxy, server} = coincident(self)

proxy.import = server.import

Here is the error message image

WebReflection commented 10 months ago

you need to enable sharedarraybuffer and all details are iin the README: https://github.com/WebReflection/coincident#coincidentserver

you can't have coincident without SharedArrayBuffer so workers are mandatory. Set the right headers and call it a day?