https://github.com/Tormund/news
news - nim easy web socket. Based on https://github.com/treeform/ws
https://github.com/Tormund/news
Last synced: about 1 month ago
JSON representation
news - nim easy web socket. Based on https://github.com/treeform/ws
- Host: GitHub
- URL: https://github.com/Tormund/news
- Owner: Tormund
- License: mit
- Created: 2019-05-08T13:06:46.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-01T08:44:19.000Z (about 2 years ago)
- Last Synced: 2025-04-04T15:01:47.595Z (3 months ago)
- Language: Nim
- Homepage:
- Size: 35.2 KB
- Stars: 36
- Watchers: 1
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: news.nimble
- License: LICENSE
Awesome Lists containing this project
- awesome-nim - news - Nim Easy WebSocket. Based on ws. (Web / Protocols)
README
# NEWS - Nim Easy WebSocket.
* Based on https://github.com/treeform/ws
* Support `asyncdispatch` and https://github.com/status-im/nim-chronos## Example Echo Server:
Example echo server, will repeat what you send it:
```nim
import news, asyncdispatch, asynchttpservervar server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
if req.url.path == "/ws":
var ws = await newWebsocket(req)
await ws.send("Welcome to simple echo server")
while ws.readyState == Open:
let packet = await ws.receivePacket()
await ws.send(packet)
await req.respond(Http200, "Hello World")waitFor server.serve(Port(9001), cb)
```## Websocket client
Send messages to Echo server and receive unswer
```nim
import news, asyncdispatchproc sendMsg() {.async.} =
var ws = await newWebSocket("ws://localhost:9001/ws")
await ws.send("hi")
while ws.readyState == Open:
let packet = await ws.receiveString()
echo "received ", packetwaitFor sendMsg()
```## Websocket with chronos support:
```nim
import chronosconst newsUseChronos = true
include newsproc sendMsg() {.async.} =
var ws = await newWebSocket("ws://localhost:9001/ws")
await ws.send("hi")
while ws.readyState == Open:
let packet = await ws.receiveString()
echo "received ", packetwaitFor sendMsg()
```## SSL/TLS connection is configured with a different prefix:
*Note: not supported for chronos variant*
```nim
var ws = await newWebSocket("wss://localhost/") # SSL context will be defaulted unless explicitly passed
```