larasopp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Laravel websocket client
2
+
3
+ ### Connect app to websocket
4
+ ```js
5
+ ...
6
+ import Larasopp from "Larasopp";
7
+
8
+ const App = () => {
9
+ ...
10
+ useEffect(() => {
11
+ Larasocket.connect({
12
+ token: 'user token',
13
+ host: 'ws://127.0.0.1:9002'
14
+ });
15
+ });
16
+ ...
17
+
18
+ ```
19
+
20
+ ### Subscribe on channel and bind event
21
+ ```js
22
+ const channel = Larasocket.subscribe('channel-1');
23
+ channel.bind('message', function(data) {
24
+ console.log(data);
25
+ });
26
+
27
+ // Unsubscribe
28
+ channel.remove();
29
+ ```
30
+
31
+ ### Trigger event on subscribe channel
32
+
33
+ ```js
34
+ Larasocket.trigger('channel-1', 'message', {
35
+ test: "hello world"
36
+ },'public');
37
+ ```
@@ -0,0 +1,5 @@
1
+ import Larasopp from "../src";
2
+
3
+ test('Connect', () => {
4
+ // todo
5
+ });
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "larasopp",
3
+ "version": "1.0.0",
4
+ "description": "Websocket client for laravel",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "jest"
8
+ },
9
+ "jest": {
10
+ "transform": {
11
+ "^.+\\.[t|j]sx?$": "babel-jest"
12
+ }
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/SergoMorello/larasopp.client.git"
17
+ },
18
+ "keywords": [
19
+ "websocket",
20
+ "laravel",
21
+ "react",
22
+ "broadcast",
23
+ "events"
24
+ ],
25
+ "author": "Sergey Serov",
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "url": "https://github.com/SergoMorello/larasopp.client/issues"
29
+ },
30
+ "homepage": "https://github.com/SergoMorello/larasopp.client#readme",
31
+ "devDependencies": {
32
+ "@babel/preset-env": "^7.20.2",
33
+ "babel-jest": "^29.4.3",
34
+ "jest": "^29.4.3"
35
+ },
36
+ "dependencies": {
37
+ "easy-event-emitter": "^1.0.3"
38
+ }
39
+ }
package/src/index.js ADDED
@@ -0,0 +1,124 @@
1
+ import EventEmitter from "easy-event-emitter";
2
+
3
+ export default class Larasopp {
4
+ static #token;
5
+ static #host;
6
+ static #socket;
7
+ static #status = false;
8
+ static #events = new EventEmitter;
9
+ static #stepReconnect = 0;
10
+
11
+ static connect(params) {
12
+ Larasopp.#host = params.host ?? '127.0.0.1';
13
+ Larasopp.#token = params.token ?? '0';
14
+ const onOpen = params.onOpen ?? (()=>{});
15
+ const onClose = params.onClose ?? (()=>{});
16
+
17
+ if (Larasopp.#status) {
18
+ return;
19
+ }
20
+ Larasopp.#socket = new WebSocket(Larasopp.#host + '/token=' + Larasopp.#token);
21
+
22
+ Larasopp.#socket.onopen = function(event) {
23
+ Larasopp.#stepReconnect = 0;
24
+ Larasopp.#status = true;
25
+ Larasopp.#events.emit("open", event);
26
+ onOpen(event);
27
+ };
28
+
29
+ Larasopp.#socket.onclose = function(event) {
30
+ if (event.wasClean) {
31
+ console.log('Соединение закрыто чисто');
32
+ } else {
33
+ if (Larasopp.#stepReconnect >= 5) {
34
+ console.log('connect error');
35
+ }else{
36
+ setTimeout(() => {
37
+ console.log('try reconnect...');
38
+ Larasopp.connect(params);
39
+ ++Larasopp.#stepReconnect;
40
+ }, 3000);
41
+ }
42
+ }
43
+ Larasopp.#status = false;
44
+ Larasopp.#events.emit("close", event);
45
+ onClose(event);
46
+ };
47
+
48
+ Larasopp.#socket.onmessage = function(event) {
49
+ if (Larasopp.#_isJsonString(event.data)) {
50
+ const json = JSON.parse(event.data);
51
+
52
+ Larasopp.#events.emit(json.channel + ':' + json.event, json.message);
53
+ }
54
+ };
55
+
56
+ Larasopp.#socket.onerror = function(error) {
57
+ Larasopp.#events.emit("error", error);
58
+ };
59
+ }
60
+
61
+ static disconnect() {
62
+ Larasopp.#socket.close();
63
+ }
64
+
65
+ static #_isJsonString(str) {
66
+ try {
67
+ JSON.parse(str);
68
+ } catch (e) {
69
+ return false;
70
+ }
71
+ return true;
72
+ }
73
+
74
+ static subscribe(channel) {
75
+ if (Larasopp.#status) {
76
+ Larasopp.send({
77
+ subscribe: channel
78
+ });
79
+ }else{
80
+ const event = Larasopp.#events.addListener('open',() => {
81
+ Larasopp.send({
82
+ subscribe: channel
83
+ });
84
+ event.remove();
85
+ });
86
+ }
87
+
88
+ return {
89
+ bind: (event, callback) => {
90
+ const retEvent = Larasopp.#events.addListener(channel + ':' + event, callback);
91
+ return {
92
+ remove: () => {
93
+ Larasopp.send({
94
+ unsubscribe: channel
95
+ });
96
+ retEvent.remove();
97
+ }
98
+ }
99
+ },
100
+ remove: () => {
101
+ Larasopp.send({
102
+ unsubscribe: channel
103
+ });
104
+ }
105
+ }
106
+ }
107
+
108
+ static trigger(channel, event, message, access) {
109
+ Larasopp.send({
110
+ channel: channel,
111
+ event: event,
112
+ message: message,
113
+ type: access ?? 'public'
114
+ });
115
+ }
116
+
117
+ static send(message, json) {
118
+ json = json ?? true;
119
+ if (!Larasopp.#status) {
120
+ return;
121
+ }
122
+ Larasopp.#socket.send(json ? JSON.stringify(message) : message);
123
+ }
124
+ }