Skip to content

Commit aeecf9e

Browse files
docs: add "emitting and listening to events" pages
Imported from the javascript documentation: - https://socket.io/docs/v3/emitting-events/ - https://socket.io/docs/v3/listening-to-events/
1 parent dee6bb9 commit aeecf9e

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Emitting events
2+
3+
See also: https://socket.io/docs/v3/emitting-events/
4+
5+
**Table of content**
6+
7+
<!-- MACRO{toc} -->
8+
9+
There are several ways to send events between the server and the client.
10+
11+
## Basic emit
12+
13+
The Socket.IO API is inspired from the Node.js [EventEmitter](https://nodejs.org/docs/latest/api/events.html#events_events):
14+
15+
*Server*
16+
17+
```js
18+
io.on("connection", (socket) => {
19+
socket.emit("hello", "world");
20+
});
21+
```
22+
23+
*Client*
24+
25+
```java
26+
socket.on("hello", new Emitter.Listener() {
27+
@Override
28+
public void call(Object... args) {
29+
System.out.println(args[0]); // world
30+
}
31+
});
32+
```
33+
34+
This also works in the other direction:
35+
36+
*Server*
37+
38+
```js
39+
io.on("connection", (socket) => {
40+
socket.on("hello", (arg) => {
41+
console.log(arg); // world
42+
});
43+
});
44+
```
45+
46+
*Client*
47+
48+
```java
49+
socket.emit("hello", "world");
50+
```
51+
52+
You can send any number of arguments, and all serializable datastructures are supported, including binary objects like [Buffer](https://nodejs.org/docs/latest/api/buffer.html#buffer_buffer) or [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray).
53+
54+
*Server*
55+
56+
```js
57+
io.on("connection", (socket) => {
58+
socket.on("hello", (...args) => {
59+
console.log(args); // [ 1, '2', <Buffer 61 62 63>, { test: '42' } ]
60+
});
61+
});
62+
```
63+
64+
*Client*
65+
66+
```java
67+
byte[] buffer = "abc".getBytes(StandardCharsets.UTF_8);
68+
JSONObject object = new JSONObject();
69+
object.put("test", "42");
70+
71+
socket.emit("hello", 1, "2", bytes, object);
72+
```
73+
74+
## Acknowledgements
75+
76+
Events are great, but in some cases you may want a more classic request-response API. In Socket.IO, this feature is named acknowledgements.
77+
78+
You can add a callback as the last argument of the `emit()`, and this callback will be called once the other side acknowledges the event:
79+
80+
*Server*
81+
82+
```js
83+
io.on("connection", (socket) => {
84+
socket.on("update item", (arg1, arg2, callback) => {
85+
console.log(arg1); // 1
86+
console.log(arg2); // { name: "updated" }
87+
callback({
88+
status: "ok"
89+
});
90+
});
91+
});
92+
```
93+
94+
*Client*
95+
96+
```java
97+
socket.emit("update item", 1, new JSONObject(singletonMap("name", "updated")), new Ack() {
98+
@Override
99+
public void call(Object... args) {
100+
JSONObject response = (JSONObject) args[0];
101+
System.out.println(response.getString("status")); // "ok"
102+
}
103+
});
104+
```
105+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Listening to events
2+
3+
See also: https://socket.io/docs/v3/listening-to-events/
4+
5+
**Table of content**
6+
7+
<!-- MACRO{toc} -->
8+
9+
There are several ways to handle events that are transmitted between the server and the client.
10+
11+
## EventEmitter methods
12+
13+
### socket.on(eventName, listener)
14+
15+
Adds the *listener* function to the end of the listeners array for the event named *eventName*.
16+
17+
```java
18+
socket.on("details", new Emitter.Listener() {
19+
@Override
20+
public void call(Object... args) {
21+
// ...
22+
}
23+
});
24+
```
25+
26+
### socket.once(eventName, listener)
27+
28+
Adds a **one-time** *listener* function for the event named *eventName*.
29+
30+
```java
31+
socket.once("details", new Emitter.Listener() {
32+
@Override
33+
public void call(Object... args) {
34+
// ...
35+
}
36+
});
37+
```
38+
39+
### socket.off(eventName, listener)
40+
41+
Removes the specified *listener* from the listener array for the event named *eventName*.
42+
43+
```java
44+
Emitter.Listener listener = new Emitter.Listener() {
45+
@Override
46+
public void call(Object... args) {
47+
calls.add("two");
48+
}
49+
};
50+
51+
socket.on("details", listener);
52+
53+
// and then later...
54+
socket.off("details", listener);
55+
```
56+
57+
### socket.off(eventName)
58+
59+
Removes all listeners for the specific *eventName*.
60+
61+
```java
62+
socket.off("details");
63+
```
64+
65+
### socket.off()
66+
67+
Removes all listeners (for any event).
68+
69+
```java
70+
socket.off();
71+
```

src/site/site.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
<menu name="Overview">
2727
<item name="Installation" href="./installation.html"/>
2828
<item name="Usage" href="./usage.html"/>
29+
<item name="Emitting events" href="./emitting_events.html"/>
30+
<item name="Listening to events" href="./listening_to_events.html"/>
2931
<item name="Migrating from 1.x" href="./migrating_from_1_x.html"/>
3032
</menu>
3133

0 commit comments

Comments
 (0)