favetelinguis
I have an ssl socket which receives messages in JSON format. Right now im using Erlang ssl to setup the connection. In my implementation handle_info is called when some data with a fixed size is available. However the JSON messages can be large so that handle_info is called multiple times with only parts of the message. I then do:
String.ends_with?(new_msg, "\n")
to put together whole messages.
This feels very slow, what I would like is to move this logic down and only have handle_info called when a complete message is available on the socket, that is when a message terminated with \n has arrived.
Is there any support for this in Erlang/Elixir?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
You can buffer the message in the process controlling the socket and only after it’s received in full, send to the process where
handle_infois defined. Or you can add a message length header to your protocol (instead of\n) and receive only what’s needed with:ssl.recv(socket, message_length, timeout)– that would be the fastest approach (you’d need to open the socket withactive: falseand:rawoptions).favetelinguis
Thanks for the tips, right now im buffering, and its not my protocol so I cant change that. The issue I have is that checking
ends_with?is O(n) in the length of the message, I was hoping there was some better way.voltone
For stream-based sockets, including
ssl, Erlang can do the buffering for you for a number of packet formats, including line protocols. The mode is controlled through thepacketoption: inet — OTP 29.0.2 (kernel 11.0.2).So, to get messages from a TLS socket delivered at line break boundaries:
:ssl.connect('example.net', 443, packet: :line)favetelinguis
The docs says the following,
Line mode, a packet is a line-terminated with newline, lines longer than the receive buffer are truncated. Does this not mean that I still need to buffer in my process since it is exactly the case where a line is longer then the receive buffer im interested in?voltone
Right, there is an upper bound, but if you know the upper limit of the messages you expect to get you can customise it using
:inet.setopts(s, buffer: @max_size).If you want to be able to receive messages of arbitrary size, you’d still have to implement buffering in your application. But even in that case using
packet: :linemakes your job easier, because it guarantees that you won’t get multiple lines (of fragments of multiple lines) in a single packet. Using:rawmode that can happen, and you’d have to scan not just for a newline at the end, but also newlines inside a message: you’d have to do the entire reassembly into lines, both joining and splitting.As for scanning for the newline at the end, if you’re using charlists it is indeed O(n), but if put the socket in binary mode and you use
String.ends_with?it should be O(1).NobbZ
Nope, it is still O(n), since elixir does also check if the full string is valid UTF-8.
If one does not care for UTF-8 validity of the string when checking for the final newline (remember, we could have a chunk split midst a multibyte codepoint anyway), then one can use
:binary.last(input) == ?\n.voltone
But does it? It is implemented as a combination of
byte_size/1andbinary_part/3, and both are not UTF-8 aware…https://github.com/elixir-lang/elixir/blob/v1.7.3/lib/elixir/lib/string.ex#L2000-L2018
NobbZ
Oh, then it has been optimized. Or was always. But I remember that @josevalim once said, all functions in
Stringwhere checking for validity.And in fact, in my opinion they should, and we should have another module working with pure binaries that does not check for string specifics, as erlang makes a different between the
stringsandlistsmodule as well…voltone
Oh, I agree, and I was going to suggest a real binary alternative when I decided to just check the source real quick anyway
favetelinguis
I dont know the exact max size but would there be any negative impact of just reserving a very large buffer like 1mb. I know nothing will be larger than that and most messages will be around 500chars.