#connecting socket and http server
1 messages ยท Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
To make the HTTP server and web sockets work together, you need to follow these steps:
-
Set up an HTTP server using a framework like Express.js or Node.js built-in
httpmodule. -
Install a web socket library like Socket.io or ws.
-
Initialize the web socket server and attach it to the same server instance as your HTTP server.
-
In your
/voteroute handler, handle the vote logic and emit the updated vote count to all connected web socket clients.
Here's an example using Express.js and Socket.io:
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
let voteCount = 0;
// Serve static files or set up other routes
app.post('/vote', (req, res) => {
// Handle vote logic
voteCount++;
// Emit updated vote count to all connected clients
io.emit('vote', voteCount);
res.sendStatus(200);
});
io.on('connection', (socket) => {
// Send initial vote count to new client
socket.emit('vote', voteCount);
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
});
In this example, when a user makes a POST request to /vote, the voteCount is incremented, and then the updated count is emitted to all connected clients using io.emit(). The initial voteCount value is also sent to new clients when they connect via the 'connection' event listener.
On the client-side, you can listen for the 'vote' event using JavaScript code like this:
const socket = io();
socket.on('vote', (count) => {
// Update the vote count on the client-side
console.log('New vote count:', count);
});
This way, whenever a new vote is received from the server, the client will update the vote count accordingly.
The only problem is idk if I can reference both ws and http server in Java
And even if I can it might be the case I need to implement both servers in different scopes or even languages so I can't have both servers in one file conveniently
Are you using a framework? And are you using some message broker rn?
If you have some sort of "topic" logic, aka all users connect to a topic and one message can be sent to all of the users, it would be very easy
using Soring!
No brokers atm but that might work
this is the visual representation
so the question mark is
my question - how to implement that connection
Mightg be broker
we use grpc for that
basically takes care of communication like that
but you could always just make a rest endpoint on the websocket server
lol thats a nonkers architecture
if your scale and size requires it get kafka in the middle 
but yeah architecturally speaking that makes no sense
if you want websocket you can do that on node (afaik its more performant than java versions), but then do all comms over it
if i had to guess, you just don't know how to set up web sockets in java
but you know how to set them up in nodejs
i see, good idea, i thought abou Redis/RabbitMQ

yes
yeah okay
but also because nodeJS is good with with sockets/it has good libs fori t
so is this a school project or just a messaround project?
messaround! im practicing and opening new things for me
for example i didnt think about grpc before
cool
and now i was recommended with that i can scale it up with a async broker
well, grpc doesn't really do anything that http doesn't
(its different - but not in any way important to this)
never rlly worked with grpc tbh justh heard ab it but yeah
(its still request/response as far as i am aware)
okay so
๐ฅ
lets learn what web sockets actually are
The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by ...
...i think you know how to use them
but at least i have 2 ways, broker/grpc and maybe database
or at least how to do a hello world
yes ofccc
I really doubt you understand them
okay then
rlly appeciate both fo your helps :3 <3
- how is a web socket connection made?
got some info to digest and dig into
through http with a 101 status
ws(s):// is the schema
then u just send lightweight msgs in bot hways
idk i think sockets are even easier than http tbh
no headers or methods, just title and body ๐คท
btw gotta try sockets with Java.
i'll leave you to it then - i do reccomend just not using node
stick to one language, one machine, one platform
even if you had two java servers that would be too complicated by just a mile
and the answer for all of this stuff is always going to be "a queue"
once a web socket connection is made, that connection is a sort of "logical actor"
its a living, breathing, organism
to get data into it, you need to direct it to it
which means at the place you get new information you need some mechanism of communicating to that "actor"
the way to do that is with queues
ofc in prod id most likely consider that, this question is rather theoretical and maybe for general knowledge ^^
whether that is in process
- ArrayBlockingQueue
- etc.
or out of process
- Kafka, SQS
- your sql db + polling
- redis
- etc
NodeJS is just rlly good with async stuff idk especially sockets
its fine
but Java,. especially Spring easily beats any http framework in JS imho
its actually not any better than java with async stuff nowadays
it used to have a performance edge until...~ 3 hours ago
but java was always the simpler programming model
hmmm maybe could be could be
(get java 21 today)
Closed the thread.
i didnt set them up another team did, and having ~300 websocket connections was a big issue for them, as their library or so spawned one thread per websocket
meanwhile our nodeJS library in my current project server 6 figure connections daily without an issue
but it also only does that 
