Commit graph

23 commits

Author SHA1 Message Date
Ankush Menat
266b2797aa refactor: use Fetch API instead of superagent 2024-04-06 21:26:45 +05:30
Ankush Menat
2a56869c99 refactor: better permission checking on sockets
- no more "can subscribe X"
- add a new util function on socket `has_permission`

Basic usage:

```js
socket.has_permission(doctype, [name]).then(() => { // do something });
```
2024-04-06 21:14:48 +05:30
Ankush Menat
4778090024 refactor!: Frappe handlers using same extensible socketio 2024-04-06 18:39:16 +05:30
Ankush Menat
a313fb90cb fix(DX): no need to specify separate hook for socketio 2024-04-06 18:28:49 +05:30
Ankush Menat
39f7e4e92e fix: avoid crashing on handler import/execution
Can't do much for events, but at least here we can ignore the errors.
2024-04-06 18:17:27 +05:30
Ankush Menat
b7f2073380 feat: Extensible SocketIO
You can now specify custom event handlers for SocketIO.

Usage:

1. In your app's hooks.py add `has_realtime_event_handlers=True` so
Framework can assume your app contains custom handler and import them.
2. Create a file called `/app/realtime/handlers.js` with single module
   export a function that will setup handlers using socket.

Here's sample code:

```js
// This is /app_root/realtime/handler.js`

function chat_app_handlers(socket) {
    socket.on("hello_chat") {
	console.log("hello world!");
    }
}

module.exports = chat_app_handlers;
```

3. Restart SocketIO server and see if it worked by sending event from
   client. In desk based app you can do
   `frappe.realtime.socket.emit("hello_chat")`

Middlewares are not yet possible... will be worked upon __some other day__ [tm]

closes https://github.com/frappe/frappe/issues/21528
2024-04-06 18:17:27 +05:30
David Arnold
640e7d79b3
feat: prepare redis subsystem for unix domain socket connections 2024-03-29 10:12:19 +01:00
Ankush Menat
a35e6b4975 fix: ignore if no cookies are sent 2024-02-12 14:19:58 +05:30
Ankush Menat
11ea7e4179 feat: socketio using authorization headers
Earlier socketio only worked in browser where browser would send cookie
(cause same domain) and hence socketio server used it to auth
connection.

This however is limited and doesn't allow simply creating socket
connection from apps.

Authorization headers on other hand are simple to implement.
2024-02-11 16:16:34 +05:30
Bernd Oliver Sünderhauf
d92f74f877
chore: Fix codestyle to satisfy linter, and typo. (#22954) 2023-10-28 17:12:39 +05:30
Faris Ansari
be85c246ff fix(socketio): send correct url for auth
in case, an app uses a different frontend stack on a different development server,
socketio should use the correct webserver port when authenticating
2023-10-26 15:27:27 +05:30
Ankush Menat
84188587fa fix: clean up socketio dynamic namespaces
While serving many sites it's best to close down namespace when last
connection is closed.

refer https://github.com/socketio/socket.io/pull/4602
2023-07-29 20:35:59 +05:30
David Arnold
7780670ae4
build(deps): update node redis client to v4 (#21797)
* build(deps): update redis client to v4 in legacy mode

* fix: node17+ - prefer ipv4

* chore: use redis client v4 api (async) and adapt error handling

* fix: timeout by exiting if not in watch mode

* fix: parse message before republishing

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
2023-07-25 14:07:46 +05:30
Ankush Menat
b5409f6f07 chore: remove eslint-disable* 2023-07-14 15:41:20 +05:30
David Arnold
98d38e7333
fix: gently log the port on which the realtime service listens (#21575)
* fix: gently log the port on which the realtime service listens

without this, the service remains completely silent (e.g. in overmind) and it is hard to tell if it is even running

* Revert "fix: gently log the port on which the realtime service listens"

This reverts commit 14a21989c6767520d8e1b9d46cc9f58a45768563.

* refactor(socketio): explicitly listen on port

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
2023-07-14 11:36:31 +05:30
Ankush Menat
be8d2b9de0 perf: lazy websocket connection on website
Establishing 1 connection for every website visit is too much.

Only after calling frappe.realtime.on(...) for ANY event, we will
establish a websocket connection.

This is used for handful of things:
- Discussion component
- File upload

Socketio was initially added here: https://github.com/frappe/frappe/pull/6866 this use case no longer exists.

Rarely anywhere website uses realtime.
2023-07-01 12:43:53 +05:30
Ankush Menat
bdaed29ef2 refactor: move handlers to separate file 2023-07-01 12:43:47 +05:30
Ankush Menat
d59e499418 refactor!: Use SocketIO namespaces for multitenancy 2023-07-01 10:53:34 +05:30
Ankush Menat
dad1d57b90 chore: move redis sub setup 2023-07-01 10:53:34 +05:30
Ankush Menat
d13cb116bc refactor: use existing redis client 2023-07-01 10:53:34 +05:30
Ankush Menat
7320d2d020 refactor: move middleware out 2023-07-01 10:53:34 +05:30
Ankush Menat
164840c32e refactor: explicitly import socketio 2023-07-01 10:53:34 +05:30
Ankush Menat
5e4e60b509 refactor: Split socketio in multiple logical files 2023-07-01 10:53:34 +05:30