You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<palign="center">Easiest Websocket library for big projects</p>
2
3
3
-
This provides a very lightweight HTTP similar API for using Websockets in Node/Browser. WebSocket code often gets confusing because of generic `.on` and `.emit` events.
4
+

4
5
5
-
## Example
6
+
## Why Another Library?
6
7
7
-
// Sending Message
8
-
const { client, router } = new RestifyWebSocket(socket)
9
-
client.post('/users', { body: {
10
-
userId: 15372
11
-
}})
12
-
client.put('/users/15372', { body: {
13
-
age: 60
14
-
}})
8
+
1. Is event based system(.emit/.on) too basic to meet your app needs making development slow?
9
+
1. Do you want to wait for server response while sending websocket message with ease?
10
+
1. Do you want to use react-query with websockets?
11
+
1. Do you want to deploy to multi-node cluster with ease using Redis without doing anything extra? (Coming Soon!)
15
12
13
+
## Introduction
16
14
17
-
// Receiving Message
18
-
const { client, router } = new RestifyWebSocket(socket)
19
-
router.post('/users', (req, res) => {
20
-
const userId = req.data.userId
21
-
// save in DB and get user
22
-
res.status(200).send(user)
15
+
`restify-websocket` provides a very lightweight HTTP similar API for using Websockets in Node/Browser. WebSocket code often gets confusing because of generic `.on` and `.emit` events. While HTTP model has REST standard for CRUD, Websockets have nothing more than socket.io(rooms, namespaces). This library provides:
16
+
17
+
1. Connect, reconnect functionality out of the box. You just start sending messages and we take care of rest.
18
+
1. If you are familiar with ExpressJS then this library you already know with few exceptions.
19
+
1. Send Request from Browser like Fetch API and get promise in return
20
+
21
+
## Examples
22
+
23
+
### HTTP Get-like Request
24
+
25
+
// Browser
26
+
export const { client, receiver, socket } = new RestifyWebSocket(SERVER_URL)
27
+
// Request, just like HTTP(axios)
28
+
const response = await client.get('/users/123', {
29
+
body
23
30
})
24
-
router.put('/users/:userId', (req, res) => {
31
+
console.log(response.data)
32
+
33
+
34
+
35
+
// Websocket Server, just like express
36
+
const { clients, router, server } = new RestifyWebSocket.Server()
37
+
router.get('/users/:userId', (req, res) => {
25
38
const userId = req.params.userId
26
-
// save in DB and get user
39
+
const user = {
40
+
userId
41
+
}
27
42
res.status(200).send(user)
28
43
})
29
44
30
-
// In browser it is recommended to pass url to RestifyWebSocket as it also handles reconnect.
45
+
### Sending data from Server
46
+
47
+
Many times this happens that Websocket server sends data to Browser without being requested by Browser. For that case, receivers are available.
48
+
49
+
// BROWSER
50
+
export const { client, receiver, socket } = new RestifyWebSocket(SERVER_URL)
0 commit comments