https://github.com/ujjwalguptaofficial/shivneri-ws-client-javascript
Shivneri framework web socket client library for javascript
https://github.com/ujjwalguptaofficial/shivneri-ws-client-javascript
crystal-lang javascript-library shivneri-framework websocket websocket-client websocket-library
Last synced: about 2 months ago
JSON representation
Shivneri framework web socket client library for javascript
- Host: GitHub
- URL: https://github.com/ujjwalguptaofficial/shivneri-ws-client-javascript
- Owner: ujjwalguptaofficial
- License: apache-2.0
- Created: 2020-03-25T14:46:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-01T18:22:53.000Z (almost 6 years ago)
- Last Synced: 2025-09-17T03:39:32.876Z (9 months ago)
- Topics: crystal-lang, javascript-library, shivneri-framework, websocket, websocket-client, websocket-library
- Language: JavaScript
- Size: 566 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/gh/ujjwalguptaofficial%2Fshivneri-ws-client-javascript) 
# shivneri-ws-client-javascript
Shivneri framework web socket client library for javascript
# Installation
* npm - npm i shivneri-ws-client
* cdn - https://cdn.jsdelivr.net/npm/shivneri-ws-client/dist/shivneri-ws-client.js
* raw file - https://github.com/ujjwalguptaofficial/shivneri-ws-client-javascript/releases
# How to use
```
var socket = new shivneriWsClient.Instance();
// subscribe to event message which will be emited from server
socket.on("message", (msg) => {
console.log("message is", msg);
})
// connect to server
await socket.init(`localhost:4000/chat`);
// emit an event message to server
socket.emit("message","Hello world")
```
# Docs
## create instance
```
var socket = new shivneriWsClient.Instance();
```
## subsribe to events
```
// subscribe to event message which will be emited from server
socket.on("message", (msg) => {
console.log("message is", msg);
})
```
## send a message to server
```
// emit an event message to server
socket.emit("message","Hello world")
```
## sunscribe to system events
```
socket.onConnected = () => {
console.log("connected")
}
socket.onDisconnected = () => {
console.log("Disconnected")
}
socket.onError = (evt) => {
console.log("error message is", evt.data)
}
```
## close connection
```
socket.close();
```
## Socket state
```
connectionState = socket.state;
if(connectionState==0){
console.log("connecting")
}
else if(connectionState==1){
console.log("connected")
}
else if(connectionState==2){
console.log("closing")
}
else if(connectionState==3){
console.log("closed")
}
```
## confiure ping pong time
By default pingInterval is `10ms` & pingTimeout is `10ms`
```
socket.init("websocket url", {
pingInterval: 5000,
pingTimeout: 5000
})
```