ghcjs-websockets alternatives and similar packages
Based on the "ghcjs" category.
Alternatively, view ghcjs-websockets alternatives based on common mentions on social networks and blogs.
9.4 0.0 ghcjs-websockets VS ghcjs-domMake Document Object Model (DOM) apps that run in any browser and natively using WebKitGtk
GHCJS DOM Hello World, an example package
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of ghcjs-websockets or a related project?
DEPRECATED!! Use ghcjs-base's native websockets!
ghcjs-websockets aims to provide a clean, idiomatic, efficient, low-level,
out-of-your-way, bare bones, concurrency-aware interface with minimal
Haskell idioms found in libraries like io-streams and the server-side
The library is mostly intended to be a low-level FFI library, with the hopes that other, more advanced libraries maybe build on the low-level FFI bindings in order to provide more advanced and powerful abstractions. Most design decisions were made with the intent of keeping things as simple as possible in order for future libraries to abstract over it.
Most of the necessary functionality is in hopefully in
Documenation is online on github pages.
import Data.Text (unpack) -- A simple echo client, echoing all incoming text data main :: IO () main = withUrl "ws://my-server.com" $ \conn -> forever $ do t <- receiveText conn putStrLn (unpack t) sendText conn t
The above code will attempt to interpret all incoming data as UTF8-encoded Text, and throw away data that does not.
conn is a
Connection, which encapsulates a websocket channel.
You can also do the same thing to interpret all incoming data as any instance
Binary --- say,
-- A simple client waiting for connections and outputting the running sum main :: IO () main = withUrl "ws://my-server.com" (runningSum 0) runningSum :: Int -> Connection -> IO () runningSum n conn = do i <- receiveData conn print (n + i) runningSum (n + i) conn
receiveData will block until the
Connection receives data that is
decodable as whatever type you expect, and will throw away all nondecodable
receive function is provided as an over-indulgent layer of abstraction
where you can receive both
Text and instances of
Binary with the same
function using typeclass magic --- for the examples above, you could use
receive in place of both
send works the same way for
If you want to, you can access the incoming data directly using the
SocketMsg sum type, which exposes either a
Text or a lazy
import Data.Text (unpack, append) import qualified Data.ByteString.Base64.Lazy as B64 main :: IO () main = withUrl "ws://my-server.com" $ \conn -> forever $ do msg <- receiveMessage putStrLn $ case msg of SocketMsgText t -> unpack $ append "Received text: " t SocketMsgData d -> "Received data: " ++ show (B64.encode d)
You can talk to multiple connections by nesting
-- Act as a relay between two servers main :: IO () main = withUrl "ws://server-1.com" $ \conn1 -> withUrl "ws://server-2.com" $ \conn2 -> forever $ do msg <- receiveMessage conn1 sendMessage conn2 msg
And also alternatively, you can manually open and close connections:
-- Act as a relay between two servers main :: IO () main = do conn1 <- openConnection "ws://server-1.com" conn2 <- openConnection "ws://server-2.com" forever $ do msg <- receiveMessage conn1 sendMessage conn2 msg closeConnection conn2 closeConnection conn1
receiveMessage and its varieties will all throw an exception if the
connection closes while they're waiting or if you attempt to receive on a
closed connection. You can handle these with mechanisms from
Control.Exception, or you can use their "maybe"-family counterparts,
receiveMessageMaybe, etc., who will return results in
Just on a success,
or return a
Nothing if the connection is closed or if receiving on a closed
You can use also
connectionClosed :: Connection -> IO Bool to check if the
Connection object is closed (or
connectionCloseReason to see why).
When closing connections, there might be some messages that were received by
the socket but never processed on the Haskell side with a
These will normally be deleted; however, you can use
withUrlLeftovers to grab a list of the raw
SocketMsgs remaining after closing.
Issues and Roadmap
Copyright (c) Justin Le 2015