#connecting socket and http server

1 messages ยท Page 1 of 1 (latest)

left thistleBOT
#

<@&987246841693360200> please have a look, thanks.

left thistleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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:

  1. Set up an HTTP server using a framework like Express.js or Node.js built-in http module.

  2. Install a web socket library like Socket.io or ws.

  3. Initialize the web socket server and attach it to the same server instance as your HTTP server.

  4. In your /vote route 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.

frank wyvern
#

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

scarlet pecan
#

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

frank wyvern
#

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

scenic bloom
#

basically takes care of communication like that

#

but you could always just make a rest endpoint on the websocket server

agile vale
scenic bloom
#

if your scale and size requires it get kafka in the middle pepekek

#

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

agile vale
#

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

frank wyvern
frank wyvern
agile vale
#

yeah okay

frank wyvern
#

but also because nodeJS is good with with sockets/it has good libs fori t

agile vale
#

so is this a school project or just a messaround project?

frank wyvern
#

messaround! im practicing and opening new things for me

#

for example i didnt think about grpc before

agile vale
#

cool

frank wyvern
#

and now i was recommended with that i can scale it up with a async broker

agile vale
#

well, grpc doesn't really do anything that http doesn't

#

(its different - but not in any way important to this)

frank wyvern
#

never rlly worked with grpc tbh justh heard ab it but yeah

agile vale
#

(its still request/response as far as i am aware)

#

okay so

#

๐Ÿฅ

#

lets learn what web sockets actually are

#
IETF Datatracker

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 ...

frank wyvern
#

nono ik ab sockets

#

but idk about grpc ๐Ÿคท gotta read a lil

agile vale
#

...i think you know how to use them

frank wyvern
#

but at least i have 2 ways, broker/grpc and maybe database

agile vale
#

or at least how to do a hello world

frank wyvern
#

yes ofccc

agile vale
#

I really doubt you understand them

frank wyvern
#

i was making socket apps before but without http

#

they are pretty simple

agile vale
#

okay then

frank wyvern
#

rlly appeciate both fo your helps :3 <3

agile vale
#
  • how is a web socket connection made?
frank wyvern
#

got some info to digest and dig into

frank wyvern
#

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.

agile vale
#

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

frank wyvern
agile vale
#

whether that is in process

  • ArrayBlockingQueue
  • etc.

or out of process

  • Kafka, SQS
  • your sql db + polling
  • redis
  • etc
frank wyvern
#

NodeJS is just rlly good with async stuff idk especially sockets

agile vale
#

its fine

frank wyvern
#

but Java,. especially Spring easily beats any http framework in JS imho

agile vale
#

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

frank wyvern
#

hmmm maybe could be could be

agile vale
#

(get java 21 today)

frank wyvern
#

i will ๐Ÿ˜Ž

#

thank you once again! Have a great day ^.^

left thistleBOT
#

Closed the thread.

scenic bloom
#

meanwhile our nodeJS library in my current project server 6 figure connections daily without an issue

#

but it also only does that LUL