Seeking feedback on peoples' web-socket usage

I’m curious as to what, who and how many people are doing things with web-sockets in Purescript, so i thought i’d make a thread here for that.

I have PureScript app that i’m working on which used to be entirely browser-based and is now going to be partly a NodeJS part and a smaller browser app. Up til now i’ve found websockets-simple to be sufficient to my needs but altho’ it does have some code to enable it to work in both browser and NodeJS modes, it doesn’t do server sockets.

Under the hood websockets-simple uses web-socket but i don’t see anything like new WebSocket.Server({port: nnnnn} in that library either, so i’m wondering, first of all, is anybody at all creating server sockets in their PureScript NodeJS apps?

And, secondly, i’m interested to hear what libraries people are using, recommending, not-recommending etc

WRT server-sockets, here’s a minimal example in JS:

const express = require('express');
const path = require('path');
const WebSocket = require('ws'); 
const app = express();

const socketServer = new WebSocket.Server({port: 57782});
socketServer.on('connection', (socketClient) => {
  console.log('connected');
  console.log('client Set length: ', socketServer.clients.size);
  socketClient.on('close', (socketClient) => {
    console.log('closed');
    console.log('Number of clients: ', socketServer.clients.size);
  });
});

AFAICT there’s no lib in the purescript ecosystem that wraps that sort of usage?

(trivially easy to wrap in FFI, i know, but by the same token, pretty easy to add to an existing PureScript web-socket library if there’s one that’s in common use)

purescript-web-socket is bindings to the W3C spec. All web libraries are bindings to some web platform spec, and not to libraries specifically. WebSocket.Server is not a web spec, and is just something that the ws library happens to implement.

1 Like

ah, so there’s enough in the purescript-web-socket API already to write socket server type applications? I will read https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers properly then and see if i can do that.

thanks!

I have no idea if there’s enough there to implement a server. You’d likely still need to glue things together with node-http, which is what I’m assuming WebSocket.Server is doing.

I believe that document is just explaining the actual protocol, if one were to implement a WebSocket server. I don’t think the web platform contains anything to ease writing a server.

It’s fine with me if just wrapping that Node library is the answer, i think. I think there’d still be value in a PR to websocket-simple to provide that wrapper to a PureScript programmer tho - so i think i’ll modify the library locally and offer that to the author if it works well enough.

So, in case anybody is interested, here’s a working example program that creates a websocket-server, extending websocket-simple using Aff

I would think it would need quite a bit more work to get it to library quality so i’m not PR-ing the original library at this time.

2 Likes